ASP. net mvc filter (1)

Source: Internet
Author: User

ASP. net mvc filter (1) Preface

In the previous article, I learned about the Controller generation process and various injection points in the generation process, according to common sense, we should explain the internal execution process of the controller and the knowledge of model binding and verification. However, the MVC Framework provides a mechanism that we can use to perform horizontal operations before the Controller method is executed. The implementation of this mechanism is a filter, in this article and subsequent sections, we will explain several filters and give a rough explanation of an execution process of the filters in the framework.

 

ASP. net mvc Filter
  • Overall object model of the filter in the system framework
  • IAuthorizationFilterAuthorization authentication filter Execution Process
  • UseIAuthorizationFilterFilter
  • IActionFilterExecution Process of the behavior Filter
  • Custom implementationIActionFilterBehavior Filter
  • Use of exception Filters

 

Overall object model of the filter in the system framework

After obtaining the Controller generated by the Controller factory and executing certain controller actions, we always need to verify some data or request information. here we need to use the filter mechanism, in the Framework, you can get a rough idea about how filters work.

Now let's take a look at the filters in the MVC framework.

Figure 1

As shown in, the Controller callsControllerActionInvokerThe type of InvokeAction () method. In the InvokeAction () method, the framework generates the Controller description object by default.ControllerDescriptorAnd controller action description objectActionDescriptorThese two types of objects are encapsulation of the information about the current controller and the Controller method to be requested. We will talk about this in the future. Here we only need to know about it, ignore their generation process. See:

Figure 2

According to the execution process of the InvokeAction () methodFilterInfoWhen it comes to the type, we all know that the MVC Framework provides us with four filters. Which of the four filters are described one by one?FilterInfoWhat is the type? Let's take a look at its object structure:

1 // encapsulate information about available operation filters. 2 public class FilterInfo 3 {4 public FilterInfo (); 5 public FilterInfo (IEnumerable <Filter> filters); 6 public IList <IActionFilter> ActionFilters {get ;} 7 public IList <IAuthorizationFilter> AuthorizationFilters {get;} 8 public IList <IExceptionFilter> predictionfilters {get;} 9 public IList <IResultFilter> ResultFilters {get;} 10}

It has four types of attributes in the Filter set, and a constructor receives the IEnumerable <Filter> type. WhenFilterInfoDuring initialization, the type is parsed Based on the Type passed in by the constructor and assigned values to the four attributes respectively. This involves another metadata description object.Filter.

Let's take a look at the structure of the Filter object:

1 // indicates a metadata class, which contains the implementation of one or more filter interfaces, the filter order, and the reference of the filter range. 2 public class Filter 3 {4 public const int DefaultOrder =-1; 5 public Filter (object instance, FilterScope scope, int? Order); 6 7 public object Instance {get; protected set;} 8 public int Order {get; protected set;} 9 public FilterScope Scope {get; protected set;} 10}

Some friends may not understand this object. How can this problem be solved? The metadata programming mode is rare. Here I will give you an example to understand it at a Glance:

1     [Authorize(Order=1)]2     public class DemoController : Controller3     {4         ……5     }

The column above will generateFilterType object, and the value of Order is equal to 1, andFilterThe Instance attribute in the type is a reference to the Authorize type Instance in the above example. This is the metadata description object. Of course, it is not very detailed and can be understood by everyone, the specific use of the Authorize type is described in the next article.

 

Now we return to the topic. As described in 2, the IEnumerable <Filter> set type is the key. How can we generate the IEnumerable <Filter> set type?

First, call the GetFilters () method in the ControllerActionInvoker type. We can see that the parameter type of the method is the context object of the controller parameter and the metadata description object of the controller action. These two objects are enough, they already contain a lot of information. In the GetFilters () method of the ControllerActionInvoker, The GetFilters () method of the FilterProviderCollection type is called internally, which is the same as the type method signature described above, but there are differences in the return type, and the actual result is generated according to the parameter execution.FilterA type object is an object that implements the IFilterProvider type,

Take a look at the structure of the IFilterProvider type:

1 // provides an interface for searching filters. 2 public interface IFilterProvider3 {4 IEnumerable <Filter> GetFilters (ControllerContext controllerContext, ActionDescriptor actionDescriptor); 5}

This object can be injected from the outside, as mentioned in controller (3), by implementing the IDependencyResolver type, in the Framework, the default implementation is also implemented. (I only did not see it through the decompilation tool. The error message is displayed, indicating that it is very depressing. Later, it is called the default implementation ). In GetFilters () of the FilterProviderCollection type, all the filter metadata description objects on the current request behavior are obtained by default and sorted and verified. Then return the IEnumerable <Filter> set type and generateFilterInfoType object.

 

IAuthorizationFilter: Authorization authentication filter Execution Process

Figure 3

Let's take a look at the definition of the IAuthorizationFilter type:

1 public interface IAuthorizationFilter 2 {3 // Abstract: 4 // called when authorization is required. 5 // 6 // parameter: 7 // filterContext: 8 // filter context. 9 void OnAuthorization (AuthorizationContext filterContext); 10}

The above definition is displayed, and the execution process of the iauthorizationfilter type is clear at a glance. According to the context object of the ControllerContext controller parameter and the Action description object actionDescriptor, The AuthorizationContext authorization authentication filter parameter context object is generated and traversed.FilterInfoType AuthorizationFilters attribute, one by one to execute our defined filter.

 

This article introduces the use of the IAuthorizationFilter type.

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.