ASP. net mvc filter (3)

Source: Internet
Author: User

ASP. net mvc filter (iii) Preface

This article describes the execution process of the behavior filter. the implementation and usage of the filter have the meaning of AOP, you can learn about the execution process of the filter in the framework to obtain some AOP knowledge (in the process of sequential execution, this programming mode is the horizontal insertion point, we still focus on learning filters. I know how to use the IAuthorizationFilter authorization authentication filter, but I cannot write it out. It contains many knowledge points. I have written half of it and deleted it. I would rather not send it to anyone, if I can master it later, I will write it out in time to share it with you. This directory is empty, which is a reminder for you.

 

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

 

Execution Process of IActionFilter

We directly go to the topic. The execution process here is followed by the part in the filter (1). Let's take a look at the execution process:

Figure 1

1 shows the entire execution process. The figure is too large to be split. This is a little more detailed (here, a sentence is provided, and many of the following content is included in it, for example, Model metadata, Model binding, and Model verification ).

Figure 2

The first is to callControllerActionInvokerThe GetParameterValues () method. The parameters of the GetParameterValues () method are the context parameter objects of the controller 【ControllerContextType and Controller Method description object 【ActionDescriptorType, and then in this methodAccordingActionDescriptorType parameter to callGetParameters ()Method to obtain the description object of the Controller method parameters 【ParameterDescriptorType.ParameterDescriptorThe type is not explained too much. You only need to know that it contains some information about the Controller method parameters, such as parameter names and parameter types.

Figure 3

As shown in figure 2ParameterDescriptorAfter a collection of types, it will traverse the set and call the GetParameterValue () method of the ControllerActionInvoker type shown in Figure 3. Note that the method is different from the method shown in figure 2 ], the GetParameterValue () method is called to generate a key-value team type object [pointed by the Red Arrow]. The key in the key-value team represents the parameter name, and the value is the parameter value, the generation process is:FirstMVCThe Framework calls the User-Defined model binder.(If a custom)[ImplementedIModelBinderInterface Type ],The user-defined model binder method is called to obtain the parameter values of the Controller method parameters. If the user-defined model binder is not found, the defaultModelThe binder binds parameters. If no matching type exists, a default value is returned.ParameterDescriptor. DefaultValue ]. (The content of the Model binder will be explained in the subsequent series)

Figure 4

After the key-value team with the parameter value information, callControllerActionInvokerType of InvokeActionMethodWithFilters () method. In this method, two parameter types are generated so thatControllerActionInvokerType of InvokeActionMethodFilter () method call. The following describes the two parameter types:

First Parameter

1 public class ActionExecutingContext : ControllerContext2     {3         public ActionExecutingContext();4         public ActionExecutingContext(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> actionParameters);5 6         public virtual ActionDescriptor ActionDescriptor { get; set; }7         public virtual IDictionary<string, object> ActionParameters { get; set; }8         public ActionResult Result { get; set; }9     }

In the above type definition, we understand thatActionExecutingContextType inheritedControllerContextType, and contains some information references. This means you can execute some custom operations before the Controller method is executed.

Second Parameter

 1     public class ActionExecutedContext : ControllerContext 2     { 3  4         public ActionExecutedContext(); 5         public ActionExecutedContext(ControllerContext controllerContext, ActionDescriptor actionDescriptor, bool canceled, Exception exception); 6  7         public virtual ActionDescriptor ActionDescriptor { get; set; } 8         public virtual bool Canceled { get; set; } 9         public virtual Exception Exception { get; set; }10         public bool ExceptionHandled { get; set; }11         public ActionResult Result { get; set; }12     }

ActionExecutedContextType andActionExecutingContextThe difference between types is that the former has two more attributes, one is used to save the exception information, and the other is used to set whether to handle the exception. This will be explained in the length of the exception filter.

Switch back to the topic and probably know the two types of definitions. Speaking of Func <ActionExecutedContext> For type parameters, the Lambda expression is set by default in the MVC Framework, and another expression is set for the Result attribute of the return type.ControllerActionInvokerType of InvokeActionMethod () method, which will be discussed later. Let's take a look at the definition of the expression, otherwise it will be a bit confusing:

1 Func<ActionExecutedContext> seed = () =>2                 {3                     new ActionExecutedContext(controllerContext,actionDescriptor, false,null) 4                         {5                             Result = this.InvokeActionMethod(controllerContext, actionDescriptor, parameters) 6                         }7                 };

After the preceding parameters are ready, you can callControllerActionInvokerType of InvokeActionMethodFilter () method, andIActionFilterType parameters are described in the previous sectionFilterInfoThe ActionFilters attribute in the type is provided and traversed. Finally, we can see that the inner of the InvokeActionMethodFilter () method isIActionFilterThe type parameter calls the OnActionExecuting () method, and then executes Func <ActionExecutedContext> Type parameter. As mentioned above, the default execution mode has been defined for this delegate type parameter, which is execution.ControllerActionInvokerType in the InvokeActionMethod () method, after this method is executedIActionFilterOnActionExecuted () method, and the final result value is returnedActionExecutedContextType.

Finally, let's take a look.IActionFilterType Structure Definition:

1 public interface IActionFilter 2 {3 // Abstract: 4 // call after the operation method is executed. 5 // 6 // parameter: 7 // filterContext: 8 // filter context. 9 void OnActionExecuted (ActionExecutedContext filterContext); 10 // 11 // Summary: 12 // called before the operation method is executed. 13 // 14 // parameter: 15 // filterContext: 16 // filter context. 17 void OnActionExecuting (ActionExecutingContext filterContext); 18}

The execution process of the behavior filter is completed. The next article will explain the application of this type of filter.

 

 

 

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.

Related Article

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.