Asp.net Mvc Framework 7 (Filter and execution sequence)

Source: Internet
Author: User
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.

  1. OnActionExecuting from the controller virtual method.

  2. OnActionExecuting applied to the Filter of the current Controller:

    First execute the base class, and then execute

  3. Execute the OnActionExecuting sequence of the filters applied to the Action:

    First execute the base class, and then execute

  4. Action Method

  5. The execution sequence of OnActionExecuted applied to the Filter of Action

    First execute the derived class, and then execute

  6. OnActionExecuted Method Applied to the Filter of the Current Controller

    First execute the derived class, and then execute

  7. OnActionExecuted

Sample visible http://quickstarts.asp.net/3-5-extensions/mvc/ActionFiltering.aspx
Asp.net Mvc Framework Series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.