Asp.net MVC custom error handling page Method

Source: Internet
Author: User
In ASP. net MVC, we can use the handleerrorattribute feature to specify how to handle the exception thrown by the action. if handleerrorattribute is set for an action, MVC displays the error view when an exception is thrown ~ /Views/shared directory.

Set handleerror attributes

You can set the following attributes to change the default processing of the handleerrorattribute feature:

Exceptiontype. specify the types of exceptions that the filter can handle. If this attribute is not specified, the filter will handle all exceptions.

View. Specify the view name to be displayed by the filter when an exception occurs.

Master. specify the name of the view master, if any.

Order. Specify the application order of the filter. If an action has multiple handleerrorattribute filters.

Specify the order attribute

If an action sets multiple handleerrorattribute, the Order attribute can be used to determine which filter to use. the value can be set to an integer from-1 (highest priority) to any positive integer to identify its priority. The larger the value, the lower the priority level. the Order attribute follows the following rules:

Filters applied to the controller are automatically applied to all actions of the controller.

If handleerrorattribute is applied to both the controller and action, as long as the order attribute value is the same, the filter on the Controller will be executed before the filter on the action will be executed.

For filters with the same order attribute, their execution order is not fixed.

If the order attribute is not specified, the default value is-1, which means that the filter will take precedence over other filters unless other filters specify order as-1.

If multiple filters are available, the first filter that can handle the exception will be called first, and the processing of the exception will end.

Get exception information in view

The ASP. Net MVC Framework stores exception information in viewdatadictionary and passes it to the error view. The model attribute of this viewdatadictionary is an instance of the predictioncontext class. This viewdata has the following keys:

Actionname: name of the target action Method

Controllername: name of the Target Controller

Exception: exception object.

Enable custom error handling

Next, enable custom error processing for the handleerrorattribute filter, open the Web. config file of the program, and add a customerrors element in the system. Web Section, as shown below:

 


<System. Web>
<Customerrors mode = "on" defaultredirect = "error"/>
</System. Web>

 

  Handle errors in the error View

Sometimes an error occurs in the error view. asp. net will display its default error page (yellow-red text), to avoid this situation, we in the web. the custom error page in the customerrors section of the config file is as follows:

 


<System. Web>
<Customerrors mode = "on" defaultredirect = "genericerrorpage.htm">
<Error statuscode = "500" Redirect = "/error.htm"/>
</Customerrors>
</System. Web>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.