Search code examples
c#asp.net-web-apiasp.net-web-api-routingattributerouting

Partially match route in WebAPI Controller


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.


Solution

  • Change your route template to [Route("v{ver}/search/{*remainingPath})]?...here the * would allow any number of segments after the search segment to be matched to your route...