How does an ASP. NET Web API select an Action based on the request? [Part 1]

Source: Internet
Author: User

Web API call requests are always for an Action method defined in an HttpController. The content of the request response comes from the execution result of calling the target Action method. After ASP. NET Web API successfully activates the target HttpController for the current request, the subsequent operation is to select the corresponding Action method for the request in the HttpController. [This Article has been synchronized to How ASP. NET Web API Works?]

HttpActionSelector

After fully understanding the HttpActionDescriptor object used to describe the Action method defined in HttpController, we will officially introduce the HttpActionSelector object used to select the target Action method. HttpActionSelector in ASP. NET Web APIs implements the IHttpActionSelector interface.

   IHttpActionSelector
 {
     ILookup<, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor);
     HttpActionDescriptor SelectAction(HttpControllerContext controllerContext);
 }

As shown in the code snippet above, the IHttpActionSelector interface defines two methods. The GetActionMapping method returns the ing between all actions and their names in the specified HttpController. The unique parameter of this method indicates the HttpControllerDescriptor object used to describe the target HttpController. The returned type is ILookup <string, HttpActionDescriptor>, the Key and Element respectively indicate the Action name and the HttpActionDescriptor object used to describe the Action method.

Because multiple Action methods with the same name can be defined in the same HttpController type, we can also use the ActionNameAttribute feature to specify the same Action name for multiple Action methods, therefore, multiple Action methods can share the same name. Therefore, the GetActionMapping method returns an ILookup <string, HttpActionDescriptor> object.

The selection of the target Action for the request is implemented in the SelectAction method. The only parameter of this method is the HttpControllerContext object that represents the context of the current HttpController, we can obtain the HttpRequestMessage object indicating the current request and the HttpRequestMessage object using ASP. the HttpRouteData generated by the NET Web API routing system. The returned HttpActionDescriptor is the description of the Action method used to process the current request.

ApiControllerActionSelector

Like the many "standardized components" we introduced earlier, ASP. NET Web API by default, the HttpActionSelector used to select the target Action is also registered in the current ServicesContainer. We can directly get the registered HttpActionSelector through ServicesContainer using the extension method GetActionSelector defined below.

    ServicesExtensions
 {
     
       IHttpActionSelector GetActionSelector( ServicesContainer services);
 }

By analyzing the definition of the DefaultServices constructor shown below, we know that the default HttpActionSelector is an object of the ApiControllerActionSelector type.

   DefaultServices : ServicesContainer
 {
     
      DefaultServices(HttpConfiguration configuration)
     {
         
         .SetSingle<IHttpActionSelector>( ApiControllerActionSelector());
     }
 }

ApiControllerActionSelector is defined in the namespace "System. Web. Http. Controllers". The basic member definitions are as follows.

   ApiControllerActionSelector : IHttpActionSelector
 {   
      ApiControllerActionSelector();
       ILookup<, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor);
       HttpActionDescriptor SelectAction(HttpControllerContext controllerContext);
 }
Valid Action Methods

Now we will focus on the implementation of the GetActionMapping method of ApiControllerActionSelector. The implementation logic is actually very simple :. What kind of "qualification" does an effective Action method have?

For an HttpController type that implements the IHttpController interface, all the MethodInfo it has must meet the following conditions at the same time to be considered as a valid Action method and be used to create the corresponding HttpActionDescriptor object:

  • It must be a public instance method.
  • The IsSpecialName attribute value of MethodInfo is False, indicating that the MethodInfo of the property member Getter and Setter will not be used to create HttpActionDescriptor ).
  • The method inherited from the ApiController type is not a valid Action method.

A ReflectedHttpActionDescriptor object set is generated together with the specified HttpControllerDescriptor. It is converted into an ILookup <string, HttpActionDescriptor> object and directly serves as the return value of the GetActionMapping method. The following code snippets basically reflect the logic for generating the httping between HttpActionDescriptor and its Action name in the GetActionMapping method.

   ApiControllerActionSelector : IHttpActionSelector
 {
     
      ILookup<, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor)
     {
         IEnumerable<ReflectedHttpActionDescriptor> actionDescriptor = 
             from method  controllerDescriptor.ControllerType.GetMethods()
              method.IsPublic && !method.IsStatic && !method.IsSpecialName&& !method.DeclaringType.IsAssignableFrom((ApiController))
             select  ReflectedHttpActionDescriptor(controllerDescriptor,method);
          actionDescriptor.ToLookup(action => action.ActionName, action => (HttpActionDescriptor)action);
     }    
 }

To avoid frequent and repeated reflection on the type of method members and affect performance, the operation defined in the code snippet above will only be executed when the GetActionMapping method is called for the first time. The generated ReflectedHttpActionDescriptor will be used for subsequent calls.

ActionMethodSelector

The ultimate goal of HttpActionSelector is to select the correct Action method from the target HttpController based on the current request. This goal is implemented in its SelectAction method. Before implementing the Action Selection Mechanism in the SelectAction method of ApiControllerActionSelector, we need to know another related object: ActionMethodSelector.

ActionMethodSelector implements the IActionMethodSelector interface with the following definitions. IActionMethodSelector is defined only in the assembly System. web. http. it defines a unique method IsValidForRequest. The two parameters of the method are the HttpControllerContext object of the current HttpController context and the MethodInfo of the Code target Action method. The Boolean value obtained by executing this method indicates whether the target Action method can be used to process the current request.

   IActionMethodSelector
 {
      IsValidForRequest(HttpControllerContext controllerContext, MethodInfo methodInfo);
 }

ASP. NET Web API only defines the only type that implements the IActionMethodSelector interface. It has the NonActionAttribute defined as follows. If this feature is applied to a method, it means that the target method is not a valid Action method. From the given code snippet, we can see that NonActionAttribute returns False directly in the IsValidForRequest method implemented.

 [AttributeUsage(AttributeTargets.Method, AllowMultiple=, Inherited=)]
    NonActionAttribute : Attribute, IActionMethodSelector
 {
      IActionMethodSelector.IsValidForRequest(HttpControllerContext controllerContext, MethodInfo methodInfo)
     {
          ;
     }
 }

For the NonActionAttribute feature, it is worth noting that :.

In the next article, we will discuss the most important content of this topic in the form of instances: ASP. NET Web API how to use HttpActionSelector to select a matching Action method to process the current request after the target HttpController is successfully activated.

How does an ASP. NET Web API select an Action based on the request? [Part 1]
How does an ASP. NET Web API select an Action based on the request? [Part II]

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.