HandleErrorAttribute Class

 

Represents an attribute that is used to handle an exception that is thrown by an action method.

Namespace:   System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Inheritance Hierarchy

System.Object
  System.Attribute
    System.Web.Mvc.FilterAttribute
      System.Web.Mvc.HandleErrorAttribute

Syntax

[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, 
    Inherited = true, AllowMultiple = true)]
public class HandleErrorAttribute : FilterAttribute, IExceptionFilter
[AttributeUsageAttribute(AttributeTargets::Class | AttributeTargets::Method, 
    Inherited = true, AllowMultiple = true)]
public ref class HandleErrorAttribute : FilterAttribute, IExceptionFilter
[<AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method,
    Inherited = true, AllowMultiple = true)>]
type HandleErrorAttribute = 
    class
        inherit FilterAttribute
        interface IExceptionFilter
    end
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method,
    Inherited := True, AllowMultiple := True)>
Public Class HandleErrorAttribute
    Inherits FilterAttribute
    Implements IExceptionFilter

Constructors

Name Description
System_CAPS_pubmethod HandleErrorAttribute()

Initializes a new instance of the HandleErrorAttribute class.

Properties

Name Description
System_CAPS_pubproperty AllowMultiple

Gets or sets a value that indicates whether more than one instance of the filter attribute can be specified.(Inherited from FilterAttribute.)

System_CAPS_pubproperty ExceptionType

Gets or sets the type of the exception.

System_CAPS_pubproperty Master

Gets or sets the master view for displaying exception information.

System_CAPS_pubproperty Order

Gets or sets the order in which the action filters are executed.(Inherited from FilterAttribute.)

System_CAPS_pubproperty TypeId

Gets the unique identifier for this attribute.(Overrides Attribute.TypeId.)

System_CAPS_pubproperty View

Gets or sets the page view for displaying exception information.

Methods

Name Description
System_CAPS_pubmethod Equals(Object)

(Inherited from Attribute.)

System_CAPS_protmethod Finalize()

(Inherited from Object.)

System_CAPS_pubmethod GetHashCode()

(Inherited from Attribute.)

System_CAPS_pubmethod GetType()

(Inherited from Object.)

System_CAPS_pubmethod IsDefaultAttribute()

(Inherited from Attribute.)

System_CAPS_pubmethod Match(Object)

(Inherited from Attribute.)

System_CAPS_protmethod MemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethod OnException(ExceptionContext)

Called when an exception occurs.

System_CAPS_pubmethod ToString()

(Inherited from Object.)

Explicit Interface Implementations

Name Description
System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.GetTypeInfoCount(UInt32)

(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

(Inherited from Attribute.)

Remarks

The HandleErrorAttribute 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 HandleErrorAttribute 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

  • Specifying the Order Property

  • Accessing Exception Data in the View

  • Enabling Custom Error Handling

  • Handling Errors in the Error View

Setting HandleErrorAttribute Properties

You can modify the default behavior of the HandleErrorAttribute 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 HandleErrorAttribute filter is possible for a method.

Specifying the Order Property

The Order property of the HandleErrorAttribute attribute helps determine which HandleErrorAttribute 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:

  1. Filters that are applied to a controller automatically apply to every action method in that controller.

  2. 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.

  3. Filters with the same order number are applied in an undetermined order.

  4. If no order number is specified, the order number is -1. This means that the filter is applied before any other HandleErrorAttribute filters, except other filters whose order is also -1.

  5. The first HandleErrorAttribute 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 HandleErrorAttribute 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>

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

System.Web.Mvc Namespace

Return to top