Like a filter in MVC
1.AuthorizeAttribute
2.ActionFilterAttribute
3.HandleErrorAttribute
First, introduce Authorizeattribute, this is the authorization filter
1. Create a class that inherits Authorizeattribute
2. Override the Onauthorization method to verify permissions, such as session and Cookie, in the method
3. Add a tag to a method or class, and if you add an attribute on a method, the Onauthorization method is called before the method that adds the attribute is executed.
If you add an attribute on a class, the Onauthorization method is called before all methods in the class are executed.
If you want to call the Onauthorization method on all methods of the Controller class, add the registration under the Registerglobalfilters method under the Filterconfig class under App_start.
1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Linq; 4 usingsystem.web; 5 usingSYSTEM.WEB.MVC; 6 7 namespaceFilter Filters8 { 9 Public classMyauthorizeattribute:authorizeattributeTen { One Public Override voidonauthorization (AuthorizationContext filtercontext) A { - //Here you can verify permissions, such as verifying session and Cookie - //can get Session with filtercontext.httpcontext.session["Key") the Base. Onauthorization (Filtercontext); - } - } -}
Second, ActionFilterAttribute use similar to Authorizeattribute, before and after executing the action and the view before and after the filter
1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Linq; 4 usingsystem.web; 5 usingSYSTEM.WEB.MVC; 6 7 namespaceFilter Filters8 { 9 Public classMyactionfilterattribute:actionfilterattributeTen { One //called before the action is loaded A Public Override voidonactionexecuting (actionexecutingcontext filtercontext) - { - //validation of permissions can also be done here the Base. OnActionExecuting (Filtercontext); - } - //called after the action is loaded - Public Override voidonactionexecuted (actionexecutedcontext filtercontext) + { - Base. OnActionExecuted (Filtercontext); + } A //Call before loading view at Public Override voidonresultexecuting (resultexecutingcontext filtercontext) - { - Base. Onresultexecuting (Filtercontext); - } - //called after the view is loaded - Public Override voidonresultexecuted (resultexecutedcontext filtercontext) in { - Base. Onresultexecuted (Filtercontext); to } + } -}
Third, Handleerrorattribute use with the above two similar, exception handling filter
1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.Linq; 4 usingsystem.web; 5 usingSYSTEM.WEB.MVC; 6 7 namespaceFilter Filters8 { 9 Public classMyhandleerrorattribute:handleerrorattributeTen { One Public Override voidonexception (exceptioncontext filtercontext) A { -Exception ex = filtercontext.exception;//Get Exception - //you can record the exception in the log. the //jump to the friendly page to avoid displaying the error page to the user -FilterContext.HttpContext.Response.Redirect ("/err.html"); - Base. Onexception (Filtercontext); - } + } -}
Filter execution Order: Authorization validation Filter--perform action before and after filter--load view before and after filter
ASP. NET MVC Filter