Analysis of Asp. Net Web API routing system --- WebHost deployment method, api --- webhost

Source: Internet
Author: User
Tags webhost

Analysis of Asp. Net Web API routing system --- WebHost deployment method, api --- webhost

In the previous article, we analyzed Asp. net routing system. Let's take a brief look at Asp. when the. Net Web API is deployed as WebHost, Asp. net Web API routing system is implemented internally. Or start with a simple instance.

Create an empty WebApi project and register route information in Global:

Public class WebApiApplication : System.Web.HttpApplication
   {
     Protected void Application_Start()
     {
       / / Register a route
       GlobalConfiguration.Configuration.Routes.MapHttpRoute(
         Name: "default",
         routeTemplate: "api/{controller}/{id}",
         Defaults: new { id = RouteParameter.Optional });
     }
   }

Create a Controller named Home:

  public class HomeController : ApiController
  {
    // GET: api/Home
    public IEnumerable<string> Get()
    {
      return new string[] { "value1", "value2" };
    }

    // GET: api/Home/5
    public string Get(int id)
    {
      return "value";
    }
    
  }

Start running. in the address bar of the browser, enter http: // localhost: 46351/api/home and http: // localhost: 46351/api/home/5. The result is as follows:

 



After a brief look at the Asp. Net Web API instance, we will start to analyze the routing system of Asp. Net Web API.

First, let's take a look at the method of registering routes in Asp. Net Web APIs, as shown below:

 

What operations are hidden during the route registration process? The following is the source code:




 

By looking at the source code, you can see that Asp. in Net Web APIs, the registration of routes is actually implemented by calling the extension method of the HttpRouteCollection type MapHttpRoute. In the MapHttpRoute method, we can see that the created route object is saved by calling the Add method of the HttpRouteCollection object. However, because the static attribute of GlobalConfiguration uses the HostedHttpRouteCollection type as the RouteTable. routes is created for the constructor. Because the HostedHttpRouteCollection type is a subclass of the HttpRouteCollection type, in the HostedHttpRouteCollection type, the HostedHttpRouteCollection class overrides the Add and CreateRoute methods of the parent type, in fact, the type of the created route object is HostedHttpRoute. This route object is saved in the global route table. From here, we can know that the type of the route object saved to the global route table is HostedHttpRoute. So what are the purposes of the registered route object stored in the global route table? Some further analysis will be conducted.


 

From the source code above, we can see that the last created route object is of the HostedHttpRoute type. Now there is a problem. When we register the route, we did not specify RouteHandler and HttpHandler, where are they added to the routing object? What are the secrets of creating a HostedHttpRoute object? We will continue to view the source code below:


 

Through the analysis above, we can know that in Asp. when a. Net Web API is hosted in the WebHost mode, the registered route object is an instance of the HostedHttpRoute type and is stored in the global route table RouteTable. in Routes, the RouteHandler and HttpHandler used to process the request are HttpControllerRouteHandler and HttpControllerHandler, respectively.

After registering the route information, how does one use the registered route information in Asp. Net Web APIs for routing? Will it be implemented through an HttpModule like in Asp. Net? Let's start the program and take a look at the Modules attribute in the Global class:

 

It can be clearly seen from the above that when Asp. Net Web APIs host services in the WebHost mode, it is also the same as ASP. Net through UrlRoutingModule to achieve routing. From the previous article for Asp. net routing system analysis, we can know that Asp. net intercepts requests through the UrlRoutingModule, and then matches the requests in sequence from the global routing table to obtain RouteData that matches the request Url for subsequent processing. In Asp. net Web API, from the above, we know that the route object stored in the global route table is of the HostedHttpRoute type. Next, we will continue to analyze it in Asp.. Net.

In UrlRoutingModule, RouteData is obtained by calling the GetRouteData method of each route object in sequence. In Asp. Net Web API, because the type of the route object is HostedHttpRoute, let's take a look at what happened when the GetRouteData method is called:

 

We can see that in HostedHttpRoute, RouteData is obtained through the GetRouteData method of the OriginalRoute attribute. From the analysis above, we know that the OriginalRoute attribute is of the HttpWebRoute type:


 

From the analysis above, we can see that when Asp. Net Web APIs are deployed in the WebHost mode, they are finally matched through the Asp. Net routing system. However, it must be noted that, because the methods of verifying the parent type are overwritten in HttpWebRoute, the validation of the constraints is Asp. net Web API still uses its own method to verify whether the constraints match:


Finally, after obtaining the RouteData object, the RouteHandler and HttpHandler contained in the RouteData through a series of work, Asp. Net Web APIs can process and respond to the requests obtained through these operations.

Summary:

Through the analysis above, we can conclude that in Asp. when the Web API is deployed as a WebHost, the registered route is saved in the global route table. When obtaining RouteData, it is obtained through Asp. the matching rules of the Net routing system are used for route matching, but their own constraint verification rules are implemented.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.


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.