The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorController.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Mail;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Teknik.Areas.Error.ViewModels;
  9. using Teknik.Controllers;
  10. using Teknik.Filters;
  11. using Teknik.Utilities;
  12. using Teknik.Logging;
  13. using Teknik.Attributes;
  14. namespace Teknik.Areas.Error.Controllers
  15. {
  16. [TeknikAuthorize]
  17. public class ErrorController : DefaultController
  18. {
  19. [TrackPageView]
  20. [AllowAnonymous]
  21. public ActionResult Exception(Exception exception)
  22. {
  23. ViewBag.Title = "Exception - " + Config.Title;
  24. ViewBag.Description = "Just a boring 'ol exception. Nothing to see here, move along.";
  25. if (Response != null)
  26. {
  27. Response.StatusCode = 500;
  28. Response.TrySkipIisCustomErrors = true;
  29. }
  30. string errorMessage = "General Exception";
  31. if (Request != null && Request.Url != null)
  32. {
  33. errorMessage += " on page: " + Request.Url.AbsoluteUri;
  34. }
  35. Logger.WriteEntry(LogLevel.Error, errorMessage, exception);
  36. ErrorViewModel model = new ErrorViewModel();
  37. model.Exception = exception;
  38. return View("~/Areas/Error/Views/Error/Exception.cshtml", model);
  39. }
  40. [TrackPageView]
  41. [AllowAnonymous]
  42. public ActionResult General(Exception exception)
  43. {
  44. ViewBag.Title = "Http Exception - " + Config.Title;
  45. ViewBag.Description = "There has been a Http exception. Run!";
  46. if (Response != null)
  47. {
  48. Response.StatusCode = 500;
  49. Response.TrySkipIisCustomErrors = true;
  50. }
  51. string errorMessage = "General HTTP Exception";
  52. if (Request != null && Request.Url != null)
  53. {
  54. errorMessage += " on page: " + Request.Url.AbsoluteUri;
  55. }
  56. Logger.WriteEntry(LogLevel.Error, errorMessage, exception);
  57. ErrorViewModel model = new ErrorViewModel();
  58. model.Description = exception.Message;
  59. model.Exception = exception;
  60. return View("~/Areas/Error/Views/Error/General.cshtml", model);
  61. }
  62. [AllowAnonymous]
  63. public ActionResult Http401(Exception exception)
  64. {
  65. ViewBag.Title = "401 - " + Config.Title;
  66. ViewBag.Description = "Unauthorized";
  67. if (Response != null)
  68. {
  69. Response.StatusCode = 401;
  70. Response.TrySkipIisCustomErrors = true;
  71. }
  72. string errorMessage = "Unauthorized";
  73. if (Request != null && Request.Url != null)
  74. {
  75. errorMessage += " for page: " + Request.Url.AbsoluteUri;
  76. }
  77. Logger.WriteEntry(LogLevel.Error, errorMessage, exception);
  78. ErrorViewModel model = new ErrorViewModel();
  79. model.Exception = exception;
  80. return View("~/Areas/Error/Views/Error/Http401.cshtml", model);
  81. }
  82. [AllowAnonymous]
  83. public ActionResult Http403(Exception exception)
  84. {
  85. ViewBag.Title = "403 - " + Config.Title;
  86. ViewBag.Description = "Access Denied";
  87. if (Response != null)
  88. {
  89. Response.StatusCode = 403;
  90. Response.TrySkipIisCustomErrors = true;
  91. }
  92. string errorMessage = "Access Denied";
  93. if (Request != null && Request.Url != null)
  94. {
  95. errorMessage += " on page: " + Request.Url.AbsoluteUri;
  96. }
  97. Logger.WriteEntry(LogLevel.Error, errorMessage, exception);
  98. ErrorViewModel model = new ErrorViewModel();
  99. model.Exception = exception;
  100. return View("~/Areas/Error/Views/Error/Http403.cshtml", model);
  101. }
  102. [AllowAnonymous]
  103. public ActionResult Http404(Exception exception)
  104. {
  105. ViewBag.Title = "404 - " + Config.Title;
  106. ViewBag.Description = "Uh Oh, can't find it!";
  107. if (Response != null)
  108. {
  109. Response.StatusCode = 404;
  110. Response.TrySkipIisCustomErrors = true;
  111. }
  112. string errorMessage = "Page Not Found";
  113. if (Request != null && Request.Url != null)
  114. {
  115. errorMessage += " for page: " + Request.Url.AbsoluteUri;
  116. }
  117. Logger.WriteEntry(LogLevel.Warning, errorMessage, exception);
  118. ErrorViewModel model = new ErrorViewModel();
  119. model.Exception = exception;
  120. return View("~/Areas/Error/Views/Error/Http404.cshtml", model);
  121. }
  122. [TrackPageView]
  123. [AllowAnonymous]
  124. public ActionResult Http500(Exception exception)
  125. {
  126. ViewBag.Title = "500 - " + Config.Title;
  127. ViewBag.Description = "Something Borked";
  128. if (Response != null)
  129. {
  130. Response.StatusCode = 500;
  131. Response.TrySkipIisCustomErrors = true;
  132. }
  133. string errorMessage = "Server Error";
  134. if (Request != null && Request.Url != null)
  135. {
  136. errorMessage += " on page: " + Request.Url.AbsoluteUri;
  137. }
  138. Logger.WriteEntry(LogLevel.Error, errorMessage, exception);
  139. ErrorViewModel model = new ErrorViewModel();
  140. model.Exception = exception;
  141. return View("~/Areas/Error/Views/Error/Http500.cshtml", model);
  142. }
  143. private string GetIPAddress()
  144. {
  145. string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  146. if (!string.IsNullOrEmpty(ipAddress))
  147. {
  148. string[] addresses = ipAddress.Split(',');
  149. if (addresses.Length != 0)
  150. {
  151. return addresses[0];
  152. }
  153. }
  154. return Request.ServerVariables["REMOTE_ADDR"];
  155. }
  156. }
  157. }