Is there any way to match only the first part of a URL using WebAPI (attribute routing specifically). Something like a way to match multiple optional path components where the number is not known beforehand.
For example: [Route("v{ver}/search/{remainingPath})]
matches the paths v1/search/products
or v2/search/customers/1234
.
I'd like to take advantage of WebAPI's great route matching framework, but the path components after search
will not be part of the controller/action matching process.
将您的路线模板更改为[Route("v{ver}/search/{*remainingPath})]
?...此处, *
允许search
段之后的任意数量的段与您的路线相匹配...
You should be able to override the route handlers to intercept the request and do custom routing. I've never done it, but it looks like you can implement the HttpControllerDispatcher and override the SendAsync method to change the request.
public class CustomRouteHandler : HttpControllerDispatcher
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
//Your code here to change the route
return base.SendAsync(request, cancellationToken);
}
}
Check out the poster for a visual of what you can override in the web api pipeline. http://www.asp.net/posters/web-api/asp.net-web-api-poster.pdf
Here's some more articles on routing that may help. http://www.asp.net/web-api/overview/web-api-routing-and-actions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.