Is it possible to customize ASP.NET Web API's routing mechanism to route all requests to the API to one controller method?
If a request comes in to
www.mysite.com/api/products/
or
www.mysite.com/api/otherResource/7
All would be routed to my SuperDuperController's Get() method?
I don't konw why you would want to do this and I certainly wouldn't recommend routing everything through one controller, however you could achieve this as follows. Assuming you are only ever going to have a resource with an optional id in your calls, add this to your WebApiConfig:
Then define your controller method as follows:
You would need to decide on an appropriate
IHttpActionResult
to return for each different type of resource.Alternatively using Attribute Routing, ensure that
config.MapHttpAttributeRoutes()
is present in your WebApiConfig and add the following attributes to your controller method: