MVC source code analysis, mvc source code
Next, let's take a look at the error processing filter.
Before reading this section, let's take a look at the functions provided by MVC.
I. built-in functions of MVC
1. configuration method
<system.web> <!--mode : Off / On / RemoteOnly--> <customErrors mode="RemoteOnly"> <error statusCode="404" redirect="~/NotFound/Index"/> </customErrors></system.web>
The mode is Off by default, indicating that the detailed error message is directly displayed on the page.
If it is On, no detailed error information is displayed, as shown in the following figure:
When the jump page is not configured with a specific status, it is displayed as follows:
If the jump page corresponding to the error status is configured below, it will jump to the page we specified in advance:
The mode here has another value: RemoteOnly. This indicates that the detailed error message is displayed on the server side, and the specified page is displayed on the client side.
2. Method for adding features to a method
Here we will first introduce several parameters of this feature.
We can see four parameters. Let's take a look at what each parameter is.
Parameters |
Description |
Predictiontype |
Exception type to be processed |
Master |
Name of the template view, which is stored under the Views/Shared File |
View |
The content view name, which is also stored in the Views/Shared file. |
Order |
The order in which the filter is applied. The smaller the value, the higher the value. The maximum value is-1. The default value is-1. |
[HandleError (ExceptionType = typeof (Exception), View = "Error500", Master = "_ Layout1")] public ActionResult Index () {throw new Exception ("exceptions actively thrown by Home/Index ");}
Ii. Custom Filters
1. In the Controller, the custom OnException Method
In HomeController, add the following two methods:
Public ActionResult Index () {throw new Exception ("Exception actively thrown by Home/Index");} protected override void OnException (ExceptionContext filterContext) {filterContext. httpContext. response. redirect ("~ /Errors/MyError? Msg = "+ filterContext. Exception. Message );}
Create an error handling controller ErrorsController and add the following method:
public ActionResult MyError(string msg){ ViewBag.Msg = msg; return View();}
At this point, the preparation work is almost done, and I will not post the view part. The result will be displayed directly:
This method works only for the methods in the controller, just like the previous permission filter.
Is there a global one, as we did before? Of course, yes.
2. Custom global/local error Filters
Create a custom filter MyErrorsAttribute
Public class MyErrorsAttribute: HandleErrorAttribute {public override void OnException (ExceptionContext filterContext) {base. onException (filterContext); // mark that this error has been handled. If other error collectors catch 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 global and local differentiation.
1). Global mode:
public class FilterConfig{ public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new MyErrorsAttribute()); }}
2). Local Mode-feature Mode
[MyErrors] public ActionResult Index () {throw new Exception ("Exception thrown by Foot/Index ");}
In error handling, you can use log4 to record errors and display friendly error pages to users.
Directory synchronized