Next, let's take a look at the error-handling filter.
Before looking at this section, let's look at what MVC has to offer.
One. MVC comes with features
1. Configuration method
<system.web> <!--mode:off/on/remoteonly - <customErrorsMode= "RemoteOnly"> <ErrorStatusCode= "404"redirect= "~/notfound/index"/> </customErrors></system.web>
The mode here is Off by default, and the detailed error message is displayed directly in the page.
If it is on, the detailed error message is not displayed, as shown here:
When there are no jump pages configured for a specific status, the display is this:
If the error status corresponding to the jump page is configured below, we will jump to our pre-specified page:
The mode here actually has a value: RemoteOnly. This means that detailed error messages are displayed on the server side, and the specified page is displayed on the client. It is still very useful.
2. Methods for adding properties to a method
Here are a few parameters for this feature.
You can see 4 parameters and see what each parameter is doing.
| Parameters |
Describe |
| Exceptiontype |
The type of exception to handle |
| Master |
The name of the template view, stored under the views/shared file |
| View |
The content view name is also stored in the views/shared file |
| Order |
The filter is applied in the order, the smaller the higher the top level is-1, the default is-1 |
[HandleErrortypeof"Error500" "_layout1" )] public actionresult Index () { thrownew Exception (" Home/index The exception that is actively thrown ");}
Two. Custom Filters
1. Controller controllers, custom onexception methods
In HomeController, add the following two methods:
Public ActionResult Index () { thrownew Exception ("home/index the exception that is actively thrown ");} protected Override void onexception (Exceptioncontext filtercontext) { filterContext.HttpContext.Response.Redirect (" ~/errors/myerror?msg=" + filterContext.Exception.Message);}
Then create an error handling controller Errorscontroller, adding the following method:
Public ActionResult myerror (string msg) { = msg; return View ();}
Here, the preparation work is almost there, the view part I will not post, directly on the result:
This approach, like the previous permission filters, only works on the methods within the controller.
Is that the same as before, there is a global? Of course, yes.
2. Custom Global/Local error filters
Create a new custom filter Myerrorsattribute
Public classmyerrorsattribute:handleerrorattribute{ Public Override voidonexception (Exceptioncontext filtercontext) {Base. Onexception (Filtercontext); //Flag This error has been processed, and if another error trap catches this error, it will not be processed again.filtercontext.exceptionhandled =true; FilterContext.HttpContext.Response.Redirect ("~/errors/myerror?msg="+filterContext.Exception.Message); }}
The next step is the global and local distinction.
1). Global mode:
Public class filterconfig{ publicstaticvoid registerglobalfilters ( Globalfiltercollection filters) { filters. ADD (new myerrorsattribute());} }
2). Local mode-Characteristics of the way
[myerrors] Public ActionResult Index () { thrownew Exception ("foot/index the exception that is actively thrown ");}
In error handling, you can use LOG4 to record errors and then display the friendly error page to the user for the purpose.
Directory is synchronized
MVC Source Analysis-Error filter