first, the role of the filter
In the MVC project, when we want to implement these functions: Authentication, exception handling, logging, performance statistics, if you follow the general approach that need to repeat the work on each page, so that not only time-consuming and laborious, the code will become redundant and difficult to understand, How do I add these features without changing the original code structure? AOP is created to solve this kind of problem. AOP, meaning: aspect-oriented programming, through the pre-compilation method and run-time dynamic agent implementation of the program without modifying the source code to add functionality dynamically unified a technology. In MVC , a filter is an AOP technique.
ii. Classification of filters
There are four types of filters supported by MVC:Authorization ( authorization ), action(behavior), Result (results) and Exception (exceptions), such as the following table
third, the use of filters (to achieve the interface mode)
1. Filter definition
Public classMyfilter:filterattribute, Iactionfilter, Iresultfilter, Iauthorizationfilter, Iexceptionfilter { Public voidonauthorization (AuthorizationContext filtercontext) {filterContext.HttpContext.Response.Write ("back to licensing <br/>"); } Public voidonactionexecuting (ActionExecutingContext filtercontext) {filterContext.HttpContext.Response.Write ( "after action execution <br/>"); } Public voidonactionexecuted (ActionExecutedContext filtercontext) {filterContext.HttpContext.Response.Write ( c7>"before action executes <br/>"); } Public voidonexception (Exceptioncontext filtercontext) {filterContext.HttpContext.Response.Write ("Return Exception:"+filtercontext.exception); Filtercontext.exceptionhandled=true;//Flag Exception handled } Public voidonresultexecuting (ResultExecutingContext filtercontext) {filterContext.HttpContext.Response.Write ( "before request execution <br/>"); } Public voidonresultexecuted (ResultExecutedContext filtercontext) {filterContext.HttpContext.Response.Write ( c9>"before request execution <br/>"); } }
2, filter use
[Myfilter] publicclass homecontroller:controller { [myfilter] Public actionresult Index () { thrownew Exception (" I am an exception ") ; return View (); } }
Note: You can add directly to a controller or action, and if you add it, only the filter on the action will be executed.
If all controller plus filters are required, it can be implemented in Filterconfig, as follows:
Public class Filterconfig { publicstaticvoid registerglobalfilters (globalfiltercollection filters ) { filters. ADD (new Handleerrorattribute ()); Filters. ADD (new myfilter); } }
Iv. Use of filters (how to inherit classes)
If you want to customize the action (behavior filter) and result (result filter), we can implement it by inheriting the class ActionFilterAttribute, overriding the methods in the class, as follows:
Public classMyfilter:actionfilterattribute { Public stringMessage {Get;Set; } Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {Base. OnActionExecuting (Filtercontext); FilterContext.HttpContext.Response.Write ("before action executes"+ Message +"<br/>"); } Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {Base. OnActionExecuted (Filtercontext); FilterContext.HttpContext.Response.Write ("after action executes"+ Message +"<br/>"); } Public Override voidonresultexecuting (ResultExecutingContext filtercontext) {Base. Onresultexecuting (Filtercontext); FilterContext.HttpContext.Response.Write ("before returning result"+ Message +"<br/>"); } Public Override voidonresultexecuted (ResultExecutedContext filtercontext) {Base. Onresultexecuted (Filtercontext); FilterContext.HttpContext.Response.Write ("After the result is returned"+ Message +"<br/>"); } }
Authorization ( authorization filter ) corresponds to the method is:authorizeattribute
Exception(Exception filter) corresponds to the method is:handleerrorattribute
Demo Download