Filter is a design based on AOP (aspect-oriented programming), and its role is to inject additional logic into the execution of ACTIONR to achieve the purpose of crosscutting injection.
IFilter
In Webapi, the filter implements the IFilter interface, and the IFilter interface has only one read-only property AllowMultiple, which indicates whether the same filter can be applied to the same target object.
public interface ifilter{BOOL AllowMultiple {get;} }
FilterInfo
The filter used by the action is also stored in Httpactiondescriptor and Httpcontrollerdescriptor. But it is not stored directly in the form of IFilter, but also with FilterInfo (System.Web.Http.Filters).
public sealed class FilterInfo {public FilterInfo (IFilter instance, filterscope scope); Public IFilter instance {get;} P Ublic filterscope Scope {get;} }
There are two properties in FilterInfo: Instance,scope. Where instance is the IFilter object. In the actual use of the filter we should pay attention to the concurrency situation, because the same filter call is the same filter object.
Filterscope
For the scope property of the FilterInfo class, it represents the domain of the filter. In Webapi, the filter has 3 domains: Global,controller,action.
public enum Filterscope { Global = 0, Controller = ten, Action =, }
The filter additions for controller,action are in the form of an attribute (Attribute). For the global filter is the filter added to the httpconfiguration.
Filterprovider
WEBAPI provides Ifilterprovider (System.Web.Http.Filters) to provide a filter under different domains.
public interface ifilterprovider{ienumerable<filterinfo> Getfilters (httpconfiguration configuration, Httpactiondescriptor actiondescriptor); }
Filterprovider is also a "standardized component" in Webapi, but it is injected in the form of multi. Webapi in IFilter has two default implementations: Configurationfilterprovider, Actiondescriptorfilterprovider, from the name we can know that the former is to provide a global filter, The latter is the filter that provides controller,action.
Class 5 Filter
WEBAPI defines a Class 5 filter for us. The following are the differences:
Authenticationfilter: Used for request authentication (Iauthenticationfilter).
Authorizationfilter: Used for Request authorization (Iauthorizationfilter).
Actionfilter:actionfilter: The registered operation is called before and after the action executes (iactionfilter).
Exceptionfilter: When the target action throws an exception is called (Iexceptionfilter).
Overridefilter: Filter (Ioverridefilter) used to mask the current domain.
This paper focuses on the following three types of filter, for the first two categories, follow-up. The default implementation for the latter three classes of filter is Actionfilterattribute,exceptionfilterattribute,overrideactionfiltersattribute. As follows:
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited = True, AllowMultiple = true)] public abstract class Actionfilterattribute:filterattr Ibute, Iactionfilter, IFilter {protected actionfilterattribute (); public virtual void onactionexecuted ( Httpactionexecutedcontext ActionExecutedContext); Public virtual Task Onactionexecutedasync (Httpactionexecutedcontext actionexecutedcontext, CancellationToken CancellationToken); public virtual void onactionexecuting (Httpactioncontext actioncontext); Public virtual Task Onactionexecutingasync (httpactioncontext actioncontext, CancellationToken cancellationtoken); }
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited = True, AllowMultiple = true)] public abstract class Exceptionfilterattribute:filtera Ttribute, Iexceptionfilter, IFilter {protected exceptionfilterattribute (); public virtual void onexception ( Httpactionexecutedcontext ActionExecutedContext); Public virtual Task Onexceptionasync (Httpactionexecutedcontext actionexecutedcontext, CancellationToken CancellationToken); }
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited = True, AllowMultiple = false)] public sealed class Overrideactionfiltersattribute:at Tribute, Ioverridefilter, IFilter {public overrideactionfiltersattribute (); public Type filterstooverride {get;} }
Uniqueness
Because filter has three fields, it is possible to inject multiple identical filter on the same action by injecting the filter in the way of the attribute designation. It is clear that this is not normally allowed in use. The corresponding policies are provided in WEBAPI to ensure the uniqueness of the filter. That is, add the AttributeUsage attribute to FilterAttribute and set the AllowMultiple to False.
Of course, after this setup you will find that in three domains I can still add the same filter, then this time Webapi will filter out a filter to call us. The filter rules are as follows:
Action>controller>global
That is, the greater the value of the Filterscope, the higher the priority.
Actionfilter
ActionFilterAttribute provides 4 virtual methods: Onactionexecuted,onactionexecutedasync,onactionexecuting,onactionexecutingasync In fact, two async methods are just the encapsulation of two synchronous methods, so we generally rewrite only two synchronous methods.
The rules for actionfilter in the order of execution are:
- Different domains: Global>controlleraction
- Same domain: executed in injection order
If you assign a value to Actioncontext.response during the execution of the onactionexecuting, the filter pipeline will do the return processing and no longer do any subsequent operations.
Exceptionfilter
In the execution action with the entire Actionfilter pipeline, if an exception is thrown,
- Expceptionfilter's execution sequence is the opposite of Actionfilter.
- The Exceptionfilter pipeline handles the exception thrown by the entire Actionfilter pipeline, and is not simply an action-thrown exception
- If an exception is thrown in the Exceptionfilter pipeline, the exceptionfilter is not executed.
Source
Github:https://github.com/barlowdu/webapi (API_13)
Overridefilter
All of the filter for an action is the sum of the filter in the Global,controller,action three domain, but sometimes we need to use the overridefilter when we want to filter on the first domain. Webapi's Overrideactionfiltersattribute will only block Iactionfilter, and no other filter will be shielded.
ASP. WebAPI Filter