Filter Applied to Action
In Asp. netMvc, you can use the Filter function when you have the following and similar requirements:
Determine whether to log on or not or user permissions, determine the output cache, anti-leeching, anti-spider, local settings, and implement dynamic actions
Filter is a declarative programming method. In Asp.net MVC, it can only be applied to Action.
Filter must inherit from the ActionFilterAttribute abstract class and overwrite void OnActionExecuting (FilterExecutingContext) and
Void OnActionExecuted (FilterExecutedContext)
OnActionExecuting is the operation before the Action is executed, while OnActionExecuted is the operation after the Action is executed.
The following is an example of the execution sequence.
First, create a Filter named TestFilter. Using System. Web. Mvc;
Namespace MvcApplication2.Controllers
{
Public class TestFilter: ActionFilterAttribute
{
Public override void OnActionExecuting (FilterExecutingContext
FilterContext ){
FilterContext. HttpContext. Session ["temp"] + = "OnActionExecuting <br/> ";
}
Public override void OnActionExecuted (FilterExecutedContext
FilterContext ){
FilterContext. HttpContext. Session ["temp"] + = "OnActionExecuted <br/> ";
}
}
}
Here we mark the execution sequence on Session ["temp "].
Write the following code in the Action in the Controller: [TestFilter]
Public void Index (){
This. HttpContext. Session ["temp"] + = "Action <br/> ";
RenderView ("Index ");
}
During the execution of this Action, we also marked the Session ["temp.
Finally, View content
It's easy to write. <% = Session ["temp"] %>
In this way, we can execute this page.
After the first execution is complete, the page displays OnActionExecuting
Action
This proves that OnActionExecuting is executed first and then Action is executed. Refresh the page.
Then OnActionExecuting
Action
OnActionExecuted
OnActionExecuting
Action
This is because OnActionExecuted is executed only after the first page is displayed, so it can be seen only when the second page is accessed.
Filter of Controller
The Filter in Monorail can be used in the Controller, which brings a lot of convenience to programmers. Can we use the Controller-level Filter in Asp.net MVC.
The implementation method is as follows: The Controller also has the OnActionExecuting and OnActionExecuted methods. You can simply rewrite them. See the code.
Namespace MvcApplication2.Controllers
{
Using System. Web. Mvc;
Public class EiceController: Controller
{
Public void Index () {this. HttpContext. Session ["temp"] + = "Action <br/> ";
RenderView ("Index ");
}
Public void Index2 (){
RenderView ("Index ");
}
Protected override void OnActionExecuting (FilterExecutingContext
FilterContext ){
FilterContext. HttpContext. Session ["temp"] + = "OnActionExecuting <br/> ";
}
Protected override void OnActionExecuted (FilterExecutedContext
FilterContext ){
FilterContext. HttpContext. Session ["temp"] + = "OnActionExecuted <br/> ";
}
}
}
Note that the two methods must be protected.
What if I want multiple controllers to use this Filter? Inherit.
Specific lifecycle of a Filter
This is a piece of official station data.
OnActionExecuting from the controller virtual method.
OnActionExecuting applied to the Filter of the current Controller:
First execute the base class, and then execute
Execute the OnActionExecuting sequence of the filters applied to the Action:
First execute the base class, and then execute
Action Method
The execution sequence of OnActionExecuted applied to the Filter of Action
First execute the derived class, and then execute
OnActionExecuted Method Applied to the Filter of the Current Controller
First execute the derived class, and then execute
OnActionExecuted
Sample visible http://quickstarts.asp.net/3-5-extensions/mvc/ActionFiltering.aspx
Asp.net Mvc Framework Series