[Boiled ASP. NET Web API2 methodology] (3-9) Air routing settings

Source: Internet
Author: User

Read Navigation

Problem

Solution Solutions

Working principle

Code Demo


In this explanation, the air route , I imagined, feel more able to express the intention of Ignoreroute, if looking at the spicy eyes ^ ^, please forgive me.

Problem

We define a centralized route in which a short-circuit is a feature of centralized routing, but now we don't want some routing to work (matching and processing requests by the routing engine). So what should we do?

Solution Solutions

In fact, the idea is very simple, just want to ignore some routes. The ASP. NET WEB API provides a processor called Stoproutinghandler, which simply says that he is a message handler that can force the specified route to be ignored. He was part of the System.Web.Http, which was introduced from Web API 2.1. The performance of the Stoproutinghandler processor on a given route is to force Httproutedispatcher to treat the route as air.

Note ASP . NET MVC have their own version of the Stoproutinghandler (located in Systme.web). If the ASP. NET Web API runs on top of a full ASP. NET runtime and has access to system.web, the same will work for the ASP.

Working principle

The ASP. NET WEB API matches all of the matching requests he can make, and if he finds those routes that are ignored, he will not deal with him. This may also be a request for a static file, which must be handled at the server level, or, if a OWIN pipeline is run, he needs to be handled by the specified OWIN middleware call.

Note OWIN Middleware is processed sequentially, and if the ASP. NET Web API is registered at the very beginning of the OWIN pipeline, it is often necessary to omit processing at the appropriate location in the ASP.

Httproutingdispatcher already in the previous [ boiled ASP. NET Web API2 Methodology](3-8) How to configure the processor for a specified route

It is mentioned that he is specialized in the message processor, and his duty is to check that the Ihttproute of the route is not matched to the current request, while the delegate handler processes the Httpcontrollerdispatcher. The httproutingdispatcher is called through Http Server.

Httproutingdispatcher checks to see if the route has a processor, and if so, continues to check if it is a stoproutinghandler type, and if so, the request will be treated as air by the routing engine. This logic is inferred from the source code of the ASP. NET WEB API, as shown in snippet 3-25.

Code Snippet 3-25. Part of the source code for ASP. NET WEB API shows how to use Stoproutinghandler and how to make air.

1 protected OverrideTaskSendAsync (httprequestmessage request, CancellationToken2 CancellationToken)3 {4Ihttproutedata Routedata =request. Getroutedata ();5     if(Routedata = =NULL)6     {7Routedata =_configuration. Routes.getroutedata (request);8         if(Routedata! =NULL)9         {Ten request. Setroutedata (routedata); One         } A     } -     if(Routedata = =NULL|| (Routedata.route! =NULL&& RouteData.Route.Handler is - Stoproutinghandler)) the     { -Request. Properties.add (httppropertykeys.noroutematched,true); -         returnTask.fromresult (Request. Createerrorresponse ( - Httpstatuscode.notfound, + Error.format (Srresources.resourcenotfound, request.) RequestUri), - srresources.noroutedata)); +     } A routedata.removeoptionalroutingparameters (); at     varInvoker = (Routedata.route = =NULL|| RouteData.Route.Handler = =NULL) -?_defaultinvoker -:NewHttpmessageinvoker (RouteData.Route.Handler, Disposehandler:false); -     returnInvoker. SendAsync (Request, cancellationtoken); -}

Code Demo

In order to configure air routing in the ASP. NET WEB API, you can use a couple of syntax results (reminds me of a couple of "back" characters in Camel Xiangzi).

    • Similar to the example in the previous article [ boiled ASP. NET Web API2 Methodology](3-8) to specify the routing configuration handler , is to use Stoproutinnghandler in the location where you want to set up the message processor in the air route. As shown in code snippet 3-26. In this example, all requests that match the/content/* will be routed by the WEB API as air, and the rest will be matched by the route defined by the/{controller}/{id}.
    • An easier way is to use the extension method Ignoreroute of the Httproutecollectionextension class. In fact the internal do is the same thing, the use of Stoproutinghandler on the specified route. As shown in code snippet 3-17.

Code Snippet 3-26. Displays the use of Stoproutinghandler to set the air route.

CONFIG. Routes.maphttproute (Name:"Content", Routetemplate:"Content/{*params}", defaults:NULL, Constraints:NULL, Handler:NewStoproutinghandler ()); Config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"{Controller}/{id}", defaults:New{id =routeparameter.optional});

Code Snippet 3-27. The air route is set through the extension method Ignoreroute.

 config. Routes.ignoreroute (routeName:       " content/{*params}  "      ); Config. Routes.maphttproute (name:   defaultapi     " {controller}/{id}  "  , defaults:  new  {ID = 

[Boiled ASP. NET Web API2 methodology] (3-9) Air routing settings

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.