Anatomy of the ASP. Webhost Deployment Method---The WEB API routing system

Source: Internet
Author: User
Tags webhost
In the previous article, we analyzed the ASP. NET routing system, and today we will briefly analyze how the ASP. NET Web API's routing system is implemented in a webhost manner. Or start with a simple instance.

Create an empty WEBAPI project to register the routing information in Global:

  public class WebApiApplication:System.Web.HttpApplication  {    protected void Application_Start ()    {      / /Register Routing      GlobalConfiguration.Configuration.Routes.MapHttpRoute (        name: "Default",        routetemplate: "api/{ Controller}/{id} ",         defaults:new {id = routeparameter.optional});    }  }

Create a controller that is named home:

  public class Homecontroller:apicontroller  {    //get:api/home public    ienumerable<string> GET () c15/>{      return new string[] {"value1", "value2"};    }    GET:API/HOME/5 public    string Get (int id)    {      return ' value ';    }      }

Start running, enter Http://localhost:46351/api/home and HTTP://LOCALHOST:46351/API/HOME/5 respectively in the browser address bar, the results are as follows:

Briefly looking at an example of the ASP. NET Web API, let's start by dissecting the routing system of the ASP.

Let's start by looking at how to register in the ASP. NET Web API, as follows:

What actions are hidden during this route registration process? Below our source code:

By looking through the source code, you can see that the registration of the ASP. NET Web API is actually implemented by calling the extension method of the Httproutecollection type Maphttproute, in the Maphttproute method, We see that the route object created was saved by calling the Add method of the Httproutecollection object. Because the static properties of Globalconfiguration are created by the Hostedhttproutecollection type in the configuration routetable.routes as the constructor parameter, and because Hostedhttproutecollec The tion type is a subclass of the httproutecollection type, in the Hostedhttproutecollection type, Subclass Hostedhttproutecollection overrides the parent type's Add method and the Createroute method, such as, so, the type of the route object that was actually created is Hostedhttproute, which is placed in the global routing table and saved. From here we can see that the type of the route object saved to the Global routing table is hostedhttproute. So, what is the use of registering a routed object in the global routing table, followed by some analysis.

From the above source can be seen, the last created Route object is the Hostedhttproute type, then there is a problem, we registered in the previous route, and did not specify the Routehandler and HttpHandler, where are they added to the route object? What are the hidden secrets of creating a Hostedhttproute object? Below we continue to view the source code:

Through the above analysis, so far, we can know that when the ASP. NET Web API is hosted in webhost, the registered routing object is an instance of the Hostedhttproute type. Saved in the Global routing table routetable.routes, while the Routehandler and HttpHandler used to process the request are instances of the Httpcontrollerroutehandler type and Httpcontrollerhandler type respectively.

After registering the routing information, How is the registered routing information used for routing in the ASP. Is it possible to do this with a httpmodule like in ASP. We start the program by looking at the Modules property in the Global class:

It is clear from the above that, when the ASP. NET Web API is hosted in webhost mode, it is the same as ASP. UrlRoutingModule for routing. From the previous anatomy of an ASP. NET routing system, we know that ASP. NET is intercepted by UrlRoutingModule and then matched sequentially from the global routing table to get the routedata that match the request URL for subsequent processing. In the ASP., we know from the above that the routing object that is saved in the Global routing table is of type hostedhttproute, and we continue to dissect how the final match is obtained in the ASP. NET Web API Routedata.

In UrlRoutingModule, Routedata is obtained by invoking the Getroutedata method of each routed object in turn. In the ASP. NET Web API, since the type of the routing object is hostedhttproute, let's look at what happens when the Getroutedata method is called:

As you can see, in Hostedhttproute the routedata is obtained through the Getroutedata method of the attribute Originalroute, which is analyzed in the previous article, We know that this Originalroute property is the Httpwebroute type:

As you can see from the above analysis, when the ASP. NET Web API is deployed in webhost mode, the matching work is eventually done through the ASP. It is important to note, however, that because of the overriding of the method of validation constraints for the parent type in Httpwebroute, the ASP is validating the constraint, and it uses its own way to verify that the constraint matches:

Finally, after acquiring the Routedata object and the Routehandler and HttpHandler contained within it through a series of work, the ASP can obtain the processing and response of the request.

Summarize:

From the above analysis, it can be concluded that when the ASP. NET Web API is deployed in webhost mode, the registered route is saved in the Global routing table, and when the routedata is obtained, is the matching rules of ASP, but it realizes its own constraint validation rules.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.