[Boiling ASP. NET Web API2 methodology] (3-9) Air routing settings, api23-9
Read navigation
Problem
Solution
Working Principle
Code demo
Here,Air RoutingI think that I think it is better to express IgnoreRoute's intention.Spicy eyes ^Sorry.
Problem
We have defined a centralized route. A feature of a centralized route is short circuit, but now we don't want some routing to work (matching and processing requests by the routing engine ). So what should we do?
Solution
In fact, the idea is very simple, that is, to ignore some routes. ASP. net web api provides a StopRoutingHandler processor. Simply put, it is a message processor that can be used to force the specified route to be ignored. It is part of System. Web. Http and has been introduced since Web API 2.1. The StopRoutingHandler processor acts on a specified route. It forces HttpRouteDispatcher to treat the route as air.
Note:ASP. NET MVCHave your own versionStopRoutingHandler(In consumer me. Web ). If ASP. net web APIs run on a full ASP. NET runtime and have the permission to access System. Web, ASP. net web APIs will also work.
Working Principle
ASP. net web api will match all the requests that can be matched. If you find that the routes are ignored, they will not process them. This may also be a request for static files. Such requests must be processed at the server level, or if an OWIN pipeline is running, it must be called and processed by the specified OWIN middleware.
Note: OWINMiddleware is ordered. If ASP. net web APIs are registered in the OWIN pipeline at the very beginning, they usually need to be ignored in the appropriate location of ASP. net web api routing.
HttpRoutingDispatcher is already in the previous article[Boiled ASP. NET Web API2Methodology](3-8) How to configure the processor for the specified route
As mentioned in, he is a dedicated message processor and is responsible for checking whether the IHttpRoute of the route matches the current request. At the same time, the delegate handler processes HttpControllerDispatcher. HttpRoutingDispatcher is called by the Http Server.
HttpRoutingDispatcher checks whether the route has a processor. If yes, it checks whether it is of the StopRoutingHandler type. If yes, the request will be treated as air by the routing engine. This logic is inferred from the source code of ASP. net web api, as shown in code snippet 3-25.
Code snippet 3-25. extracted from the ASP. net web api source code shows how to use StopRoutingHandler and how to make it air.
1 protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken 2 cancellationToken) 3 { 4 IHttpRouteData routeData = request.GetRouteData(); 5 if (routeData == null) 6 { 7 routeData = _configuration.Routes.GetRouteData(request); 8 if (routeData != null) 9 {10 request.SetRouteData(routeData);11 }12 }13 if (routeData == null || (routeData.Route != null && routeData.Route.Handler is14 StopRoutingHandler))15 {16 request.Properties.Add(HttpPropertyKeys.NoRouteMatched, true);17 return Task.FromResult(request.CreateErrorResponse(18 HttpStatusCode.NotFound,19 Error.Format(SRResources.ResourceNotFound, request.RequestUri),20 SRResources.NoRouteData));21 }22 routeData.RemoveOptionalRoutingParameters();23 var invoker = (routeData.Route == null || routeData.Route.Handler == null)24 ? _defaultInvoker25 : new HttpMessageInvoker(routeData.Route.Handler, disposeHandler: false);26 return invoker.SendAsync(request, cancellationToken);27 }
Code demo
To configure air routing in ASP. net web APIs, you can use two syntax results (several methods of writing back in Camel Xiangzi ).
- And the previous article[Boiled ASP. NET Web API2Methodology](3-8) How to configure the processor for the specified routeThe example in is similar, that is, to use StopRoutinngHandler at the location where the message processor in the air route is to be set. See code snippet 3-26. In this example, all requests that can match/content/* are treated as air routes by WEB APIs, the rest will still be matched by the routes defined by/{controller}/{id.
- An easier way is to use the IgnoreRoute Extension Method of the HttpRouteCollectionExtension class. In fact, the same thing is done internally. Use StopRoutingHandler on the specified route. See code snippet 3-17.
Code snippet 3-26. Use StopRoutingHandler to set the air route.
config.Routes.MapHttpRoute( name: "Content", routeTemplate: "content/{*params}", defaults: null, constraints: null, handler: new StopRoutingHandler()); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new {id = RouteParameter.Optional} );
Code snippet 3-27. Use the extension method IgnoreRoute to set the air route.
config.Routes.IgnoreRoute( routeName: "Content", routeTemplate: "content/{*params}" ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new {id = RouteParameter.Optional} );