Original reproduced from http://www.cnblogs.com/iamlilinfeng/archive/2013/03/02/2940162.html
Objective of this article
One, the ability to use AOP in control to implement non-business requirements
Directory of this document
First, ActionFilterAttribute class
II. implementation of custom attribute
First, ActionFilterAttribute class action filter criteria base class
1 usingSystem;2 3 namespaceSYSTEM.WEB.MVC4 {5 //Summary:6 //represents the base class for filter attributes.7[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited =true, AllowMultiple =false)]8 Public Abstract classActionfilterattribute:filterattribute, Iactionfilter, Iresultfilter9 {Ten //Summary: One //Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class. A protectedActionFilterAttribute (); - - //Summary: the //called by the ASP. NET MVC Framework after the action method executes. - // - //Parameters: - //Filtercontext: + //The filter context. - Public Virtual voidonactionexecuted (ActionExecutedContext filtercontext); + // A //Summary: at //called by the ASP. NET MVC Framework before the action method executes. - // - //Parameters: - //Filtercontext: - //The filter context. - Public Virtual voidonactionexecuting (ActionExecutingContext filtercontext); in // - //Summary: to //called by the ASP. NET MVC Framework after the action result executes. + // - //Parameters: the //Filtercontext: * //The filter context. $ Public Virtual voidonresultexecuted (ResultExecutedContext filtercontext);Panax Notoginseng // - //Summary: the //called by the ASP. NET MVC framework before the action result executes. + // A //Parameters: the //Filtercontext: + //The filter context. - Public Virtual voidonresultexecuting (ResultExecutingContext filtercontext); $ } $}
OnActionExecuting: Executes the method before the action executes
OnActionExecuted: Executes the method after the action executes
Onresultexecuting: Executes the method before result execution
Onresultexecuted: Executes the method after result execution
Second, the realization of the custom attribute on the basis of the MVC framework to implement the custom attribute only need to implement ActionFilterAttribute in the virtual method can be
1. Code
1 usingSYSTEM.WEB.MVC;2 3 namespaceMVC3. Demo.app_code4 {5 Public classLogactionfilter:actionfilterattribute6 {7 Public stringLogMessage {Get;Set; }8 9 Public Override voidonactionexecuting (actionexecutingcontext filtercontext)Ten { OneFilterContext.HttpContext.Response.Write (@"execute before Action executes"+ LogMessage +"<br/>"); A Base. OnActionExecuting (filtercontext); - } - the Public Override voidonactionexecuted (actionexecutedcontext filtercontext) - { -FilterContext.HttpContext.Response.Write (@"executes after the action executes"+ LogMessage +"<br/>"); - Base. OnActionExecuted (filtercontext); + } - + Public Override voidonresultexecuting (resultexecutingcontext filtercontext) A { atFilterContext.HttpContext.Response.Write (@"execute before result execution"+ LogMessage +"<br/>"); - Base. Onresultexecuting (filtercontext); - } - - Public Override voidonresultexecuted (resultexecutedcontext filtercontext) - { inFilterContext.HttpContext.Response.Write (@"executes after result is executed"+ LogMessage +"<br/>"); - Base. Onresultexecuted (filtercontext); to } + } -}
1. Role
1 " Log Write: Validation method " )]2public actionresult Validation ()3 {4 return View (); 5 }
3. Effects
Using AOP in the control of MVC