There are four types of filters in ASP.
Action
1, in the ASP. NET MVC project, create a new folder filter, then create a new class Mycustormfilter, inherit from the ActionFilterAttribute class, we look at the ActionFilterAttribute class has the following four methods , from the naming I should be able to see their timing of execution.
public class Mycustormfilter:actionfilterattribute {public override void OnActionExecuting ( ActionExecutingContext filtercontext) { filterContext.HttpContext.Response.Write ("Before Action Execution"); Base. OnActionExecuting (Filtercontext); } public override void OnActionExecuted (ActionExecutedContext filtercontext) { FilterContext.HttpContext.Response.Write ("After Action execution"); Base. OnActionExecuted (Filtercontext); } }
2, for the filter, we can add them in three places, one is above the controller (all the action under the controller), one is the action above (the designated action), and the other is the global position (action in all controllers). Here I only show the action above
[Mycustormfilter]
Public ActionResult Index ()
{
return View ();
}
Public ActionResult Index1 ()
{
return View ();
3. Build and Run
Result
1, new class Testresultfilter, inherit ActionFilterAttribute
public class Testresultfilter:actionfilterattribute {//<summary>//// </before loading "view" Summary> //<param name= "Filtercontext" ></param> public override void Onresultexecuting ( System.Web.Mvc.ResultExecutingContext filtercontext) { FilterContext.HttpContext.Response.Write (" Execute onresultexecuting <br/> "before loading view); Base. Onresultexecuting (Filtercontext); } <summary>/////\ </summary>// <param name= "Filtercontext" after loading "view" ></ param> public override void Onresultexecuted (System.Web.Mvc.ResultExecutedContext filtercontext) { filterContext.HttpContext.Response.Write ("Execute onresultexecuted <br/> after loading view"); Base. Onresultexecuted (Filtercontext); } }
2, here I add testresultfilter filter on the controller
[Testresultfilter] public class Filtertestcontroller:controller { [mycustormfilter] public actionresult Index () { return View (); } Public ActionResult Index1 () { return View (); } }
3, run to see the results
Note: The result filter is executed regardless of the return type of the action (or even void).
The current request matching routing information and routing objects are saved in the Routedata
Modify MyCustormFilter.cs
public override void OnActionExecuting (ActionExecutingContext filtercontext) {//1. Gets the class name and method name of the GET request String Strcontroller = filtercontext.routedata.values["Controller"]. ToString (); String straction = filtercontext.routedata.values["Action"]. ToString ();//2. Another way to get the requested class name and method name is string strAction2 = FilterContext.ActionDescriptor.ActionName; string strController2 = FilterContext.ActionDescriptor.ControllerDescriptor.ControllerName; FilterContext.HttpContext.Response.Write ("Action </br> before Execution"); FilterContext.HttpContext.Response.Write ("Controller:" + Strcontroller + "</br>"); FilterContext.HttpContext.Response.Write ("Controller:" + strcontroller2+ "</br>"); FilterContext.HttpContext.Response.Write ("Action:" + straction + "</br>"); FilterContext.HttpContext.Response.Write ("Action:" + strAction2 + "</br>"); Base. OnActionExecuting (Filtercontext); }
Authorizeattribute
1. New Testauthorizeattribute
<summary> //Authorization Filter--Execute before the action filter ///</summary> public class Testauthorizeattribute: Authorizeattribute {public override void Onauthorization (AuthorizationContext filtercontext) { FilterContext.HttpContext.Response.Write ("<br/>OnAuthorization<br/>"); Comment out the parent class method, because the Onauthorization method in the parent class invokes the ASP-AUTH mechanism! //base. Onauthorization (Filtercontext); }}
2. Add testauthorize tag to index on controller filtertest
[Mycustormfilter] [Testauthorize] Public ActionResult Index () { return View (); }
Run to see the results:
Exception
1. New TestHandleError.cs
<summary>/// exception handling Filter/// </summary> public class testhandleerror:handleerrorattribute< c6/>{public override void Onexception (Exceptioncontext filtercontext) { //1. Get exception Object Exception ex = filtercontext.exception; 2. Record the Exception Log //3. redirect Friendly page filtercontext.result = new Redirectresult ("~/error.html"); 4. Mark exception has been processed filtercontext.exceptionhandled = true; Base. Onexception (Filtercontext); } }
2. Add Testhandleerror on action
[Testhandleerror] Public ActionResult Geterr () { int a = 0; int b = 1/a; return View (); }
Note: Usually such exception handling is placed above the global filter.
public class Filterconfig {public static void Registerglobalfilters (globalfiltercollection filters) { //filters. ADD (New Handleerrorattribute ()); Add Global filter filters. ADD (New Testhandleerror ()); } }
There are four types of filter in ASP.