Objective
In the previous section, we explained in detail the process of creating the filter and introduced a rough introduction to five filters, which are important for achieving interception at all times of the action method. In this section we will simply describe the execution of custom attributes on the action method, on the controller, globally, and on authorization.
Apicontroller
Before talking about the Apicontroller, we will introduce a little bit, this section we will detail this Web API controller base class:
1 Public Abstract classApicontroller:ihttpcontroller, IDisposable2 {3 // Fields4 Privatehttpconfiguration _configuration;5 PrivateHttpcontrollercontext _controllercontext;6 Private BOOL_disposed;7 Privatemodelstatedictionary _modelstate;8 Privatehttprequestmessage _request;9 PrivateUrlhelper _urlhelper;Ten One //Methods A protectedApicontroller (); - Public voidDispose (); - protected Virtual voidDispose (BOOLdisposing); the Public VirtualTaskExecuteasync (Httpcontrollercontext controllercontext, CancellationToken cancellationtoken); - protected Virtual voidInitialize (Httpcontrollercontext controllercontext); - Internal StaticFunc<taskinvokeactionwithactionfilters(Httpactioncontext Actioncontext, CancellationToken cancellationtoken, ienumerable<iactionfilter> filters, Func<taskinneraction); - Internal StaticFunc<taskinvokeactionwithauthorizationfilters(Httpactioncontext Actioncontext, CancellationToken CancellationToken, ienumerable<iauthorizationfilter> Filters, func<taskinneraction); + Internal StaticTaskinvokeactionwithexceptionfilters(taskfilters); - + //Properties A PublicHttpconfiguration Configuration {Get;Set; } at PublicHttpcontrollercontext ControllerContext {Get;Set; } - PublicModelstatedictionary Modelstate {Get; } - PublicHttprequestmessage Request {Get;Set; } - PublicUrlhelper URL {Get;Set; } - PublicIPrincipal User {Get; } - in //Nested Types - Private classfiltergrouping to { + // Fields - PrivateList<iactionfilter>_actionfilters; the PrivateList<iauthorizationfilter>_authorizationfilters; * PrivateList<iexceptionfilter>_exceptionfilters; $ Panax Notoginseng //Methods - PublicFiltergrouping (ienumerable<filterinfo>filters); the Private Static voidCategorize<t> (IFilter filter, list<t> List)whereT:class; + A //Properties the PublicIenumerable<iactionfilter> Actionfilters {Get; } + PublicIenumerable<iauthorizationfilter> Authorizationfilters {Get; } - PublicIenumerable<iexceptionfilter> Exceptionfilters {Get; } $ } $}
Let's first look at a private class in this class filtergrouping , as the name implies, is grouping filters, and we look at their constructors to see:
Public filtergrouping (ienumerable<filterinfo> filters) { this._actionfilters = new List<iactionfilter > (); This._authorizationfilters = new list<iauthorizationfilter> (); This._exceptionfilters = new list<iexceptionfilter> (); foreach (filterinfo info in filters) { IFilter instance = info. Instance; Categorize<iactionfilter> (instance, this._actionfilters); Categorize<iauthorizationfilter> (instance, this._authorizationfilters); Categorize<iexceptionfilter> (instance, this._exceptionfilters);} }
We just need _actionfilters as an example, and the rest, let's look at the categorize method:
1 Private Static voidCategorize<t> (IFilter filter, list<t> List)whereT:class2 {3T Item = Filter asT;4 if(Item! =NULL)5 {6 list. ADD (item);7 }8}
From here we can learn:
when we initialize in Httpactiondescriptor, we create a list of the collections of filterinfo that encapsulate the filter object, and then take advantage of the three attribute types in this class: Iactionfilter, Iauthorizationfilter, and Iexceptionfilter filter groups to get a list of corresponding filter sets
Execution Process Principle Analysis
Let's take a look at the execution of the procedure below, and we'll customize the following five filters
<summary>//Global behavior Filter///</summary> public class Customconfigurationactionfilterattribute:fi Lterattribute, Iactionfilter {public taskThe next step is to implement the filter, configure the global filter in the configuration file
Config. Filters.add (New Customconfigurationactionfilterattribute ());
Controller and method on filter
[Customcontrollerauthorizationfilter] [Customcontrolleractionfilter] public class Productcontroller:apicontroller { [customactionfilter] [ Customcontrolleractionauthorizationfilter] public string GetFilter () { var sb = new StringBuilder (); var actionselector = this. Configuration.Services.GetActionSelector (); var actiondesciptor = Actionselector.selectaction (this. ControllerContext); foreach (Var filterinfo in Actiondesciptor.getfilterpipeline ()) { sb. Appendline ("FilterName:" + filterInfo.Instance.GetType (). Name + ", Filterscope:" + filterInfo.Scope.ToString () + "" "); } Return SB. ToString (); } }
Finally, check out the results:
See here is not a bit confused how to sort by global->controller->action, if you read the previous article will know that this is the filter pipeline according to Filterscope to generate, in fact, the server-side generated in the order of Customcontrollerauthorizationfilterattribute , customcontrolleractionauthorizationfilterattribute , customconfigurationactionfilterattribute , customcontrolleractionfilterattribute and Customactionfilterattribute Thus we conclude that:
authorization filters, regardless of any filterscope, are superior to the behavior filter, whereas in the same type of filter, the order of execution is determined by Filterscope.
SummarizeMore in-depth content is no longer explored, I would like to write a little more, but the state of the poor and more complicated to avoid saying that not quite understand foggy, think or forget, so, the following or give a detailed implementation of the source "Filter execution Process"
Next, we will explain in detail the authentication (authentication) and authorization (Authorization) in the Web API, and look forward to ...Web API Filter Execution Process principle Analysis "two" (11)