Explain the sequence of execution of the ASP. NET Core MVC Filter

Source: Internet
Author: User
Tags httpcontext

The ASP. NET Core MVC filter is triggered at various stages of the request pipeline. At the same stage, multiple ranges of filters can be registered, such as global range, controller range, and so on. Taking Actionfilter as an example, let's look at the trigger order of the filters.

Filter can register range
    • Global: Action that will be made for all requests
    • Controller: All actions that will act on this controller
    • Action: Acting on a single action
Define Filter Global
 Public classglobalactionfilter:iasyncactionfilter{ Public AsyncTask Onactionexecutionasync (actionexecutingcontext context, Actionexecutiondelegatenext) {varFactory = context. Httpcontext.requestservices.getservice<iloggerfactory>(); varLogger = Factory. Createlogger<globalactionfilter>(); Logger. Logwarning ("before the global actionfilter is executed"); awaitnext (); Logger. Logwarning ("after the global actionfilter is executed"); }}
Controller (divided into annotation mode and rewrite method)

Annotation method:

 Public classcontrolleractionfilter:actionfilterattribute{ Public Override AsyncTask Onactionexecutionasync (actionexecutingcontext context, actionexecutiondelegate next) {varFactory = context. Httpcontext.requestservices.getservice<iloggerfactory>(); varLogger = Factory. Createlogger<globalactionfilter>(); Logger. Logwarning ("Controlleractionfilter before execution"); awaitnext (); Logger. Logwarning ("Controlleractionfilter after execution"); }}

Override method (rewrite Onactionexecutionasync within controller)

 Public Override Async Task Onactionexecutionasync (actionexecutingcontext context,actionexecutiondelegate next) {    var logger = _factory. Createlogger<valuescontroller>();    Logger. Logwarning ("Pre-actionfinter of controller internal override ");     await next ();    Logger. Logwarning ("after the controller internally rewrites the Actionfinter executed ");}
Apply Filter Global (modified in startup)
 Public void configureservices (iservicecollection services) {    = =    {        O.filters.add< Globalactionfilter>();    });}
Controller (Overrides are directly available without action)
[Controlleractionfilter]  Public class Valuescontroller:controller
Action
[Httpget][actionactionfilter]  Public ienumerable<string> Get ()
Execution results

As you can see from the results, Controller overrides > Global > Controller > Action

Custom Filter Execution Order

The order in which the filters are executed is not immutable, and the AspNet Core allows us to manually customize the order in which the filters are executed by using a flexible design. To implement a custom filter execution order, you need to implement the Iorderedfilter interface, which defines an order property of type int, and the larger the property, the slower the execution order.

Implementing the Iorderfilter Interface
 Public class globalactionfilter:iasyncactionfilter,iorderedfilter{    publicint Order = 0;
Inherit attribute method directly into parameters
[Controlleractionfilter (Order =-1publicclass  Valuescontroller:controller {
=-2)] public ienumerable<string> Get () {
Results

Order of execution at this time: Controller internal rewrite > Action > Controller > Global

Explain the sequence of execution of the ASP. NET Core MVC Filter

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.