1
/* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
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.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System
.Web
.Mvc
{
15 using System
.Diagnostics
.CodeAnalysis
;
16 using System
.Globalization
;
18 using System
.Web
.Mvc
.Resources
;
20 [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",
21 Justification
= "This attribute is AllowMultiple = true and users might want to override behavior.")]
22 [AttributeUsage(AttributeTargets
.Class
| AttributeTargets
.Method
, Inherited
= true, AllowMultiple
= true)]
23 public class HandleErrorAttribute
: FilterAttribute
, IExceptionFilter
{
25 private const string _defaultView
= "Error";
27 private Type _exceptionType
= typeof(Exception
);
28 private string _master
;
31 public Type ExceptionType
{
33 return _exceptionType
;
37 throw new ArgumentNullException("value");
39 if (!typeof(Exception
).IsAssignableFrom(value)) {
40 throw new ArgumentException(String
.Format(CultureInfo
.CurrentUICulture
,
41 MvcResources
.ExceptionViewAttribute_NonExceptionType
, value.FullName
));
44 _exceptionType
= value;
48 public string Master
{
50 return _master
?? String
.Empty
;
59 return (!String
.IsNullOrEmpty(_view
)) ? _view
: _defaultView
;
66 public virtual void OnException(ExceptionContext filterContext
) {
67 if (filterContext
== null) {
68 throw new ArgumentNullException("filterContext");
71 // If custom errors are disabled, we need to let the normal ASP.NET exception handler
72 // execute so that the user can see useful debugging information.
73 if (filterContext
.ExceptionHandled
|| !filterContext
.HttpContext
.IsCustomErrorEnabled
) {
77 Exception exception
= filterContext
.Exception
;
79 // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
81 if (new HttpException(null, exception
).GetHttpCode() != 500) {
85 if (!ExceptionType
.IsInstanceOfType(exception
)) {
89 string controllerName
= (string)filterContext
.RouteData
.Values
["controller"];
90 string actionName
= (string)filterContext
.RouteData
.Values
["action"];
91 HandleErrorInfo model
= new HandleErrorInfo(filterContext
.Exception
, controllerName
, actionName
);
92 filterContext
.Result
= new ViewResult
{
95 ViewData
= new ViewDataDictionary
<HandleErrorInfo
>(model
),
96 TempData
= filterContext
.Controller
.TempData
98 filterContext
.ExceptionHandled
= true;
99 filterContext
.HttpContext
.Response
.Clear();
100 filterContext
.HttpContext
.Response
.StatusCode
= 500;
102 // Certain versions of IIS will sometimes use their own error page when
103 // they detect a server error. Setting this property indicates that we
104 // want it to try to render ASP.NET MVC's error page instead.
105 filterContext
.HttpContext
.Response
.TrySkipIisCustomErrors
= true;