1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Diagnostics.CodeAnalysis;
\r
16 using System.Globalization;
\r
18 using System.Web.Mvc.Resources;
\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
25 private const string _defaultView = "Error";
\r
27 private Type _exceptionType = typeof(Exception);
\r
28 private string _master;
\r
29 private string _view;
\r
31 public Type ExceptionType {
\r
33 return _exceptionType;
\r
36 if (value == null) {
\r
37 throw new ArgumentNullException("value");
\r
39 if (!typeof(Exception).IsAssignableFrom(value)) {
\r
40 throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture,
\r
41 MvcResources.ExceptionViewAttribute_NonExceptionType, value.FullName));
\r
44 _exceptionType = value;
\r
48 public string Master {
\r
50 return _master ?? String.Empty;
\r
57 public string View {
\r
59 return (!String.IsNullOrEmpty(_view)) ? _view : _defaultView;
\r
66 public virtual void OnException(ExceptionContext filterContext) {
\r
67 if (filterContext == null) {
\r
68 throw new ArgumentNullException("filterContext");
\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
77 Exception exception = filterContext.Exception;
\r
79 // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
\r
81 if (new HttpException(null, exception).GetHttpCode() != 500) {
\r
85 if (!ExceptionType.IsInstanceOfType(exception)) {
\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
94 MasterName = Master,
\r
95 ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
\r
96 TempData = filterContext.Controller.TempData
\r
98 filterContext.ExceptionHandled = true;
\r
99 filterContext.HttpContext.Response.Clear();
\r
100 filterContext.HttpContext.Response.StatusCode = 500;
\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