Home > Article > Backend Development > Analysis of Asp.Net Web API Routing System---WebHost Deployment Method
In the previous article, we analyzed the Asp.Net routing system. Today we will briefly analyze how the routing system of Asp.Net Web API is implemented internally when Asp.Net Web API is deployed in WebHost mode. Let's start with a simple example.
Create an empty WebApi project and register routing information in Global:
public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { //注册路由 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 and enter http://localhost:46351/api/home and http://localhost:46351/api in the browser address bar. /home/5, the results are as follows:
##After briefly looking at the example of Asp.Net Web API, Next we start to analyze the routing system of Asp.Net Web API. First, let’s take a look at how to register routes in Asp.Net Web API, as follows: What operations are hidden in this route registration process? ? Below is our source code:
#By looking at the source code It can be seen that the registration of routes in Asp.Net Web API is actually implemented by calling the extension method MapHttpRoute of the HttpRouteCollection type. In the MapHttpRoute method, we see that the created route object is saved by calling the Add method of the HttpRouteCollection object. Because the static properties of GlobalConfiguration are created by the HostedHttpRouteCollection type with RouteTable.Routes as the construction parameter through Configuration, and because the HostedHttpRouteCollection type is a subclass of the HttpRouteCollection type, in the HostedHttpRouteCollection type, the subclass HostedHttpRouteCollection overrides the Add method and CreateRoute of the parent type. The method is as shown below. Therefore, the type of the routing object actually created is HostedHttpRoute. This routing object is saved in the global routing table. From here we can know that the type of routing object saved in the global routing table is HostedHttpRoute. . So, what is the use of saving registered routing objects in the global routing table? This will be analyzed in the following part.
As you can see from the source code above, the last created routing object is of HostedHttpRoute type, so now there is a problem, we registered it earlier When routing, RouteHandler and HttpHandler are not specified. Where are they added to the routing object? What are the hidden secrets in the process of creating HostedHttpRoute objects? Let’s continue to view the source code:
Through the above analysis, so far, we can know that when Asp.Net Web API is hosted in WebHost mode, the registered routing object is an instance of HostedHttpRoute type, which is saved in the global routing table RouteTable.Routes, and using The RouteHandler and HttpHandler used to process requests are instances of the HttpControllerRouteHandler type and HttpControllerHandler type instances respectively.
After registering the routing information, how to use the registered routing information for routing in Asp.Net Web API? 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 screenshot above It can be seen that when Asp.Net Web API hosts services in WebHost mode, routing is implemented through UrlRoutingModule just like ASP.Net. From the previous analysis of the Asp.Net routing system, we can know that Asp.Net intercepts the request through the UrlRoutingModule, and then matches it sequentially from the global routing table to obtain the RouteData that matches the request Url for subsequent processing. of. In Asp.Net Web API, from the above we know that the routing object saved in the global routing table is of HostedHttpRoute type. Let's continue to analyze how to finally obtain the matching RouteData in Asp.Net Web API.
In UrlRoutingModule, RouteData is obtained by calling the GetRouteData method of each routing object in sequence. In Asp.Net Web API, since the type of routing object is HostedHttpRoute, let’s take a look at what happens when the GetRouteData method is called:
As you can see, in HostedHttpRoute RouteData is obtained through the GetRouteData method of the property OriginalRoute. From the previous analysis, we know that this OriginalRoute property is of the HttpWebRoute type:
As can be seen from the above analysis, when Asp.Net Web API is deployed in WebHost mode, the matching work is ultimately completed through Asp.Net's routing system. However, it should be noted that since the method of verifying constraints of the parent type has been rewritten in HttpWebRoute, Asp.Net Web API still uses its own method to verify whether the constraints match:
Finally, after obtaining the RouteData object and the RouteHandler and HttpHandler contained in it through a series of work, Asp.Net Web API can use these to process the request. and responded.
Summary:
Through the above analysis, it can be concluded that when Asp.Net Web API is deployed in WebHost mode, the registered route is saved in the global route in the table; when obtaining RouteData, route matching is performed through the matching rules of the Asp.Net routing system, but its own constraint verification rules are implemented.
The above is the entire content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will support the PHP Chinese website.
For more articles analyzing the Asp.Net Web API routing system---WebHost deployment method, please pay attention to the PHP Chinese website!