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