The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ErrorController.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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)
  114. {
  115. if (Request.Url != null)
  116. {
  117. errorMessage += " for page: " + Request.Url.AbsoluteUri;
  118. }
  119. errorMessage += " - using Method: " + Request.HttpMethod;
  120. }
  121. Logger.WriteEntry(LogLevel.Warning, errorMessage, exception);
  122. ErrorViewModel model = new ErrorViewModel();
  123. model.Exception = exception;
  124. return View("~/Areas/Error/Views/Error/Http404.cshtml", model);
  125. }
  126. [TrackPageView]
  127. [AllowAnonymous]
  128. public ActionResult Http500(Exception exception)
  129. {
  130. ViewBag.Title = "500 - " + Config.Title;
  131. ViewBag.Description = "Something Borked";
  132. if (Response != null)
  133. {
  134. Response.StatusCode = 500;
  135. Response.TrySkipIisCustomErrors = true;
  136. }
  137. string errorMessage = "Server Error";
  138. if (Request != null && Request.Url != null)
  139. {
  140. errorMessage += " on page: " + Request.Url.AbsoluteUri;
  141. }
  142. Logger.WriteEntry(LogLevel.Error, errorMessage, exception);
  143. ErrorViewModel model = new ErrorViewModel();
  144. model.Exception = exception;
  145. return View("~/Areas/Error/Views/Error/Http500.cshtml", model);
  146. }
  147. private string GetIPAddress()
  148. {
  149. string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  150. if (!string.IsNullOrEmpty(ipAddress))
  151. {
  152. string[] addresses = ipAddress.Split(',');
  153. if (addresses.Length != 0)
  154. {
  155. return addresses[0];
  156. }
  157. }
  158. return Request.ServerVariables["REMOTE_ADDR"];
  159. }
  160. }
  161. }