Represents an attribute that is used to handle an exception that is thrown by an action method.
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
Thetype exposes the following members.
|AllowMultiple||Gets or sets a value that indicates whether more than one instance of the filter attribute can be specified. (Inherited from FilterAttribute.)|
|ExceptionType||Gets or sets the type of the exception.|
|Master||Gets or sets the master view for displaying exception information.|
|Order||Gets or sets the order in which the action filters are executed. (Inherited from FilterAttribute.)|
|TypeId||Gets the unique identifier for this attribute. (Overrides Attribute::TypeId.)|
|View||Gets or sets the page view for displaying exception information.|
|Equals||(Inherited from Attribute.)|
|Finalize||(Inherited from Object.)|
|GetHashCode||(Inherited from Attribute.)|
|GetType||(Inherited from Object.)|
|IsDefaultAttribute||(Inherited from Attribute.)|
|Match||(Inherited from Attribute.)|
|MemberwiseClone||(Inherited from Object.)|
|OnException||Called when an exception occurs.|
|ToString||(Inherited from Object.)|
The attribute in ASP.NET MVC lets you specify how to handle an exception that is thrown by an action method. By default, when an action method with the attribute throws any exception, MVC displays the Error view that is located in the ~/Views/Shared folder.
This topic contains the following sections:
Setting HandleErrorAttribute Properties
You can modify the default behavior of the filter by setting the following properties:
ExceptionType. Specifies the exception type or types that the filter will handle. If this property is not specified, the filter handles all exceptions.
View. Specifies the name of the view to display.
Master. Specifies the name of the master view to use, if any.
Order. Specifies the order in which the filters are applied, if more than one filter is possible for a method.
Specifying the Order Property
The Order property of the attribute helps determine which filter is used to handle an exception. You can set the Order property to an integer value that specifies a priority from -1 (highest priority) to any positive integer value. The greater the integer value is, the lower the priority of the filter is. The Order property follows these rules:
Filters that are applied to a controller automatically apply to every action method in that controller.
Filters that are applied to the controller run before filters that are applied to an action method, as long as the order numbers are the same.
Filters with the same order number are applied in an undetermined order.
If no order number is specified, the order number is -1. This means that the filter is applied before any other filters, except other filters whose order is also -1.
The first filter that can handle the exception will be called, after which exception handling stops for that exception.
Accessing Exception Data in the View
The MVC framework passes information about an exception to the error view in the ViewDataDictionary object whose Model property is set to an instance of the ExceptionContext class. The ViewData dictionary contains values for the following keys:
ActionName. The intended action method.
ControllerName. The intended controller.
Exception. The exception object.
Enabling Custom Error Handling
To enable custom error handling for use by a filter, add a customErrors element to the system.web section of the application's Web.config file, as shown in the following example:
<system.web> <customErrors mode="On" defaultRedirect="Error" /> </system.web>
Handling Errors in the Error View
Errors can occur in the error view itself. In that case, the default ASP.NET error page is displayed. To avoid this, you can configure the application to display an error file in the customErrors section of the Web.config file, as shown in the following example:
<system.web> <customErrors mode="On" defaultRedirect="GenericErrorPage.htm"> <error statusCode="500" redirect="/Error.htm" /> </customErrors> </system.web>
The following example shows how to use the class to customize error handling for controllers and action methods. In the example, the HomeController class includes the ThrowException action method, which throws an ApplicationException error. This method is marked with a attribute that has no parameters. When the method is called, the ThrowException method throws an exception and the default Error view is displayed.
The ThrowNotImplemented method is marked with a attribute that has two parameters. The View parameter specifies which CustomErrorView instance to display. The ExceptionType parameter specifies that this filter should be called only for a NotImplementedException error.
The Order parameter of the attribute for the controller has been set to 2. This means that it will be called only if an exception occurs in the Index or About methods.
This example also shows the content of CustomErrorView and CustomError.Master, views, and shows an updated Index view. CustomErrorView displays information about the exception, such as the name of the controller and action method that threw the exception, the exception message, and the stack-trace data.
In this example, the Index view contains links to the ThrowException and ThrowNotImplemented methods.
The following example shows the HomeController class.
The following example shows the CustomErrorView view.
The following example shows the CustomError master view.
The following example shows the Index view.