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