In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the filters of ASP.NET MVC". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the filters of ASP.NET MVC"?
1. Filter
Although the four types of filters provided by ASP.NETMVC have their own implemented interfaces, for the filter delivery system, all filters are represented by a Filter type with the following definition. The core of Filter is the Instance attribute, because it represents the object that actually implements the filtering function, which implements one or more interfaces based on the above four filter types.
1:publicclassFilter
2: {
3:publicconstintDefaultOrder=-1
4:publicFilter (objectinstance,FilterScopescope,int?order)
5:
6:publicobjectInstance {get;protectedset;}
7:publicintOrder {get;protectedset;}
8:publicFilterScopeScope {get;protectedset;}
9:}
10:
11:publicenumFilterScope
12: {
13:Action=30
14:Controller=20
15:First=0
16:Global=10
17:Last=100
18:}
II. FilterProvider
The provision mechanism of Filter is similar to the previous provision mechanism based on ModelBinder and ModelValidator, which is provided through the corresponding Provider. The FilterProvider that provides the filter implements the interface IFilterProvider, as shown in the following code snippet, which defines the unique method GetFilters to get a collection of Filter objects based on the specified Controller context and the ActionDescriptor object used to describe the target Action.
1:publicinterfaceIFilterProvider
2: {
3:IEnumerableGetFilters (ControllerContextcontrollerContext
ActionDescriptoractionDescriptor)
4:}
ASP.NETMVC has several types of filters
III. FilterAttribute and FilterAttributeFilterProvider
We usually define filters as properties that are applied declaratively to Controller types or Action methods, while the abstract type FilterAttribute is the base class for all filters. As shown in the following code snippet, the FilterAttribute feature implements the IMvcFilter interface, which defines two read-only properties, Order and AllowMultiple, that control the order in which filters are executed and that multiple similar filters can be applied to the same target element (class or method) at the same time.
1: [AttributeUsage (AttributeTargets.Method | AttributeTargets.Class,Inherited=true,AllowMultiple=false)] 2IMvcFilter 3: {4:protectedFilterAttribute (); 5:6:publicboolAllowMultiple {get;} 7:publicintOrder {get;set;} 8:} 9:10:publicinterfaceIMvcFilter11: {12:boolAllowMultiple {get;} 13:intOrder {get;} 14:}
4. Controller and ControllerInstanceFilterProvider
When it comes to ASP.NETMVC filters, most of them only think of using the FilterAttribute feature. In fact, Controller itself (inherited from the abstract class Controller) is a filter. As the following code snippet shows, the abstract class Controller implements four interfaces, IActionFilter, IAuthorizationFilter, IExceptionFilter, and IResultFilter, that correspond to different filter types.
1:publicabstractclassController:ControllerBase
2:IActionFilter
3:IAuthorizationFilter
4:IExceptionFilter
5:IResultFilter
6:...
7: {
8Rank / omitted member
9:}
The FilterProvider type for a unique filter such as a Controller object is ControllerInstanceFilterProvider with the following definition. In the implemented GetFilters method, it gets the corresponding Controller object based on the specified Controller context and creates a Filter (the Controller object is the value of the Instance property of the Filter object). The Scope of this Filter is not Controller, but First, and the value of Order is-2147483648 (Int32.MinValue). There is no doubt that such a Filter must be the first to be executed.
At this point, I believe you have a deeper understanding of "what are the filters of ASP.NET MVC?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.