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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. LogError(LogLevel.Error, "General Exception", exception);
  31. ErrorViewModel model = new ErrorViewModel();
  32. model.Exception = exception;
  33. return View("~/Areas/Error/Views/Error/Exception.cshtml", model);
  34. }
  35. [TrackPageView]
  36. [AllowAnonymous]
  37. public ActionResult General(Exception exception)
  38. {
  39. ViewBag.Title = "Http Exception - " + Config.Title;
  40. ViewBag.Description = "There has been a Http exception. Run!";
  41. if (Response != null)
  42. {
  43. Response.StatusCode = 500;
  44. Response.TrySkipIisCustomErrors = true;
  45. }
  46. LogError(LogLevel.Error, "General HTTP Exception", exception);
  47. ErrorViewModel model = new ErrorViewModel();
  48. model.Description = exception.Message;
  49. model.Exception = exception;
  50. return View("~/Areas/Error/Views/Error/General.cshtml", model);
  51. }
  52. [AllowAnonymous]
  53. public ActionResult Http401(Exception exception)
  54. {
  55. ViewBag.Title = "401 - " + Config.Title;
  56. ViewBag.Description = "Unauthorized";
  57. if (Response != null)
  58. {
  59. Response.StatusCode = 401;
  60. Response.TrySkipIisCustomErrors = true;
  61. }
  62. LogError(LogLevel.Error, "Unauthorized", exception);
  63. ErrorViewModel model = new ErrorViewModel();
  64. model.Exception = exception;
  65. return View("~/Areas/Error/Views/Error/Http401.cshtml", model);
  66. }
  67. [AllowAnonymous]
  68. public ActionResult Http403(Exception exception)
  69. {
  70. ViewBag.Title = "403 - " + Config.Title;
  71. ViewBag.Description = "Access Denied";
  72. if (Response != null)
  73. {
  74. Response.StatusCode = 403;
  75. Response.TrySkipIisCustomErrors = true;
  76. }
  77. LogError(LogLevel.Error, "Access Denied", exception);
  78. ErrorViewModel model = new ErrorViewModel();
  79. model.Exception = exception;
  80. return View("~/Areas/Error/Views/Error/Http403.cshtml", model);
  81. }
  82. [AllowAnonymous]
  83. public ActionResult Http404(Exception exception)
  84. {
  85. ViewBag.Title = "404 - " + Config.Title;
  86. ViewBag.Description = "Uh Oh, can't find it!";
  87. if (Response != null)
  88. {
  89. Response.StatusCode = 404;
  90. Response.TrySkipIisCustomErrors = true;
  91. }
  92. LogError(LogLevel.Warning, "Page Not Found", exception);
  93. ErrorViewModel model = new ErrorViewModel();
  94. model.Exception = exception;
  95. return View("~/Areas/Error/Views/Error/Http404.cshtml", model);
  96. }
  97. [TrackPageView]
  98. [AllowAnonymous]
  99. public ActionResult Http500(Exception exception)
  100. {
  101. ViewBag.Title = "500 - " + Config.Title;
  102. ViewBag.Description = "Something Borked";
  103. if (Response != null)
  104. {
  105. Response.StatusCode = 500;
  106. Response.TrySkipIisCustomErrors = true;
  107. }
  108. LogError(LogLevel.Error, "Server Error", exception);
  109. ErrorViewModel model = new ErrorViewModel();
  110. model.Exception = exception;
  111. return View("~/Areas/Error/Views/Error/Http500.cshtml", model);
  112. }
  113. private void LogError(LogLevel level, string message, Exception exception)
  114. {
  115. if (Request != null)
  116. {
  117. if (Request.Url != null)
  118. {
  119. message += " | Url: " + Request.Url.AbsoluteUri;
  120. }
  121. if (Request.UrlReferrer != null)
  122. {
  123. message += " | Referred Url: " + Request.Url.AbsoluteUri;
  124. }
  125. message += " | Method: " + Request.HttpMethod;
  126. message += " | User Agent: " + Request.UserAgent;
  127. }
  128. Logger.WriteEntry(level, message, exception);
  129. }
  130. }
  131. }