2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.Web.Mvc / System.Web.Mvc / HandleErrorAttribute.cs
blob0555051e42de7960ae5946d965cbd39e4c160367
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 {
14 using System;
15 using System.Diagnostics.CodeAnalysis;
16 using System.Globalization;
17 using System.Web;
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;
29 private string _view;
31 public Type ExceptionType {
32 get {
33 return _exceptionType;
35 set {
36 if (value == null) {
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 {
49 get {
50 return _master ?? String.Empty;
52 set {
53 _master = value;
57 public string View {
58 get {
59 return (!String.IsNullOrEmpty(_view)) ? _view : _defaultView;
61 set {
62 _view = value;
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) {
74 return;
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),
80 // ignore it.
81 if (new HttpException(null, exception).GetHttpCode() != 500) {
82 return;
85 if (!ExceptionType.IsInstanceOfType(exception)) {
86 return;
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 {
93 ViewName = View,
94 MasterName = Master,
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;