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