The previous section describes the entire API runtime framework, namely, three layers of hosting, message handler pipeline, and controller handling. This section describes one of the hosts where the WebHost is hosted in the traditional asp.net pipeline.
Routing)
On the asp.net platform, a route is generally added by the RouteTables. Routes static attribute and its type is RouteCollection. For example, the Code for adding a route is provided in the following MVC template.
protected void Application_Start(){ RegisterRoutes(RouteTable.Routes);} public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } );}
Most routing logic is in the UrlRoutingModule and belongs to the PostResolveRequestCache asp.net pipeline event. For each request, this module re-matches the route set and obtains a RouteData instance. If yes:
1. instantiate RouteData and get a route handler (HttpControllerRouteHandler ).
2. Get an http handle from routehandler and inherit it from the IHttphandler interface. IRouteHandler interface method: IHttpHandler GetHttpHandler (RequestContext requestContext)
3. Finally, the current request context is mapped to the httphandler above.
Therefore, the final result is that the asp.net pipeline request is processed by this handler.
Web API Integration
When the host is on asp.net, the specific Configuration of Web API is defined in an HttpConfiguration object in singleton mode and accessed through the static attribute GlobalCnfiguration. Configuration.
The Web API also defines a new RouteCollection Extension Method MapHttpRoute to register special routes for Web APIs. The following is the configuration example code:
HttpConfiguration config = GlobalConfiguration.Configuration;config.Routes.MapHttpRoute("default", "{controller}/{id}", new {id = UrlParameter.Optional});// other configuration settings
Note:
When a route is added through MapHttpRoute and matches a Request, HttpControllerRouteHandler creates a new HttpControllerHandler inherited from IAsyncHttpHandler, Which is initialized through RouteData (including route information.
When called, HttpControllerhandler performs the following actions in his BeginReocessRequest method:
- Create an HttpRequestMessage instance for the current context
- Use GlobalConfiguration. Configuration to obtain the Configuration, create an HttpServer, and send the HttpRequestMessage to the service pipeline.
After the request is accepted by HttpServer, it enters the independent processing stage of the host (new Web API pipeline)
The following class diagram summarizes the routing solution process and is allocated to HttpServer (message processing pipeline)
Original article address:
ASP. NET Web API: web hosting
The text description is obscure and can be easily understood based on the source code.
HttpApplication-> UrlRoutingModule-> RouteCollection-> RouteData-> HttpControllerRouteHandler-> HttpControllerHandler-> HttpRequestMessage-> HttpServer
Add some source code:
1. UrlRoutingModule obtains RouteData
= (RouteData === (routeHandler = InvalidOperationException (. format (CultureInfo. currentCulture, SR. getString (), [(routeHandler === (httpHandler = InvalidOperationException (. format (CultureInfo. currentUICulture, SR. getString (),(! (HttpHandler HttpException (, SR. GetString (View Code
2. MapHttpRoute (RouteCollectionExtension)
Route MapHttpRoute (RouteCollection routes, name, routeTemplate, defaults, (routes = Error. argumentNull (=== (HostedHttpRoute) GlobalConfiguration. configuration. routes. createRoute (routeTemplate, defaultsDictionary, constraintsDictionary, dataTokens: =View Code
3. HttpControllerRouteHandler. GetHandler
View Code
4. HttpControllerHandler. ProcessRequest
= ContextBase. GetHttpRequestMessage ()?? === (Response! =View Code
5. HttpApplication-> HttpControllerHandler
(RouteData = Error. ArgumentNull (handler = Error. ArgumentNull (=View Code
HttpServer: DelegatingHandler: HttpMessageHandler