MVC exception handling mechanism

Source: Internet
Author: User

Method one: The Web. config configuration file is added under the system.web contact, if on it will not feed the exception to the user, but a friendly jump to error.htm

<customerrors mode= "on" defaultredirect= "error.htm" >
<error statuscode= "404" redirect= "~/error/notfound" ></error>
</customErrors>

Method Two: In FilterConfig.cs there is a new handleerrorattribute () such a sentence, this class for Microsoft default already have exception processing class, but is annotated, method and the priority is higher than the method.

Method Three: Custom Exception handling Classes

It should be noted here that, and the method of two differences is not small, is to achieve iexceptionfilter in the pretext of the Onexception method, can be considered to do some after the exception of the subsequent processing.

Invocation can be done by

public static void Registerglobalfilters (Globalfiltercollection filters)
{

Filters. ADD (New Customexceptionattribute (), 1);
Filters. ADD (New Handleerrorattribute (), 2);//The smaller the number, the higher the priority.
}

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Web;

Using SYSTEM.WEB.MVC;

Namespace Web

{

public class Customexceptionattribute:filterattribute, Iexceptionfilter

{

public void Onexception (Exceptioncontext filtercontext)

{

if (filtercontext.exceptionhandled = = True)

{

HttpException Httpexce = filtercontext.exception as HttpException;

if (Httpexce.gethttpcode () = 500)//Why special emphasis 500 because MVC handles HttpException, if 500 will automatically set its exceptionhandled to True, Then we can't catch the anomaly.

{

Return

}

}

HttpException HttpException = filtercontext.exception as HttpException;

if (httpexception! = null)

{

FilterContext.Controller.ViewBag.UrlReferrer = FilterContext.HttpContext.Request.UrlReferrer;

if (httpexception.gethttpcode () = = 404)

{

FilterContext.HttpContext.Response.Redirect ("~/home/notfound");

}

else if (httpexception.gethttpcode () = = 500)

{

FilterContext.HttpContext.Response.Redirect ("~/home/internalerror");

}

}

Write Log records

filtercontext.exceptionhandled = true;//Set exception already handled

}

}

}

MVC exception handling mechanism

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.