PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web.Mvc2/System.Web.Mvc/HandleErrorAttribute.cs

https://github.com/iainlane/mono
C# | 119 lines | 84 code | 17 blank | 18 comment | 11 complexity | f1bc372679ec3b2009e3b13b69d11ad4 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * This software is subject to the Microsoft Public License (Ms-PL).
  6. * A copy of the license can be found in the license.htm file included
  7. * in this distribution.
  8. *
  9. * You must not remove this notice, or any other, from this software.
  10. *
  11. * ***************************************************************************/
  12. namespace System.Web.Mvc {
  13. using System;
  14. using System.Diagnostics.CodeAnalysis;
  15. using System.Globalization;
  16. using System.Web;
  17. using System.Web.Mvc.Resources;
  18. [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",
  19. Justification = "This attribute is AllowMultiple = true and users might want to override behavior.")]
  20. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
  21. public class HandleErrorAttribute : FilterAttribute, IExceptionFilter {
  22. private const string _defaultView = "Error";
  23. private readonly object _typeId = new object();
  24. private Type _exceptionType = typeof(Exception);
  25. private string _master;
  26. private string _view;
  27. public Type ExceptionType {
  28. get {
  29. return _exceptionType;
  30. }
  31. set {
  32. if (value == null) {
  33. throw new ArgumentNullException("value");
  34. }
  35. if (!typeof(Exception).IsAssignableFrom(value)) {
  36. throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture,
  37. MvcResources.ExceptionViewAttribute_NonExceptionType, value.FullName));
  38. }
  39. _exceptionType = value;
  40. }
  41. }
  42. public string Master {
  43. get {
  44. return _master ?? String.Empty;
  45. }
  46. set {
  47. _master = value;
  48. }
  49. }
  50. public override object TypeId {
  51. get {
  52. return _typeId;
  53. }
  54. }
  55. public string View {
  56. get {
  57. return (!String.IsNullOrEmpty(_view)) ? _view : _defaultView;
  58. }
  59. set {
  60. _view = value;
  61. }
  62. }
  63. public virtual void OnException(ExceptionContext filterContext) {
  64. if (filterContext == null) {
  65. throw new ArgumentNullException("filterContext");
  66. }
  67. if (filterContext.IsChildAction) {
  68. return;
  69. }
  70. // If custom errors are disabled, we need to let the normal ASP.NET exception handler
  71. // execute so that the user can see useful debugging information.
  72. if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) {
  73. return;
  74. }
  75. Exception exception = filterContext.Exception;
  76. // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
  77. // ignore it.
  78. if (new HttpException(null, exception).GetHttpCode() != 500) {
  79. return;
  80. }
  81. if (!ExceptionType.IsInstanceOfType(exception)) {
  82. return;
  83. }
  84. string controllerName = (string)filterContext.RouteData.Values["controller"];
  85. string actionName = (string)filterContext.RouteData.Values["action"];
  86. HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
  87. filterContext.Result = new ViewResult {
  88. ViewName = View,
  89. MasterName = Master,
  90. ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
  91. TempData = filterContext.Controller.TempData
  92. };
  93. filterContext.ExceptionHandled = true;
  94. filterContext.HttpContext.Response.Clear();
  95. filterContext.HttpContext.Response.StatusCode = 500;
  96. // Certain versions of IIS will sometimes use their own error page when
  97. // they detect a server error. Setting this property indicates that we
  98. // want it to try to render ASP.NET MVC's error page instead.
  99. filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
  100. }
  101. }
  102. }