ASP. NET Web API: Web host

Source: Internet
Author: User
Tags configuration settings web hosting

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

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.