I have two controllers, ItemsController
and SingleItemController
which both inherit from ApiController
. Both have RoutePrefix items/inventory
defined on the controllers, like so:
[RoutePrefix("items/inventory")]
I'm using AttributeRouting in Web API 2.2.
In SingleItemController
I have the following route:
[Route("{itemReference}")]
[HttpGet]
public ItemDTO GetItem(string itemReference)
In ItemsController
I have the following route:
[Route("all")]
[HttpGet]
public List<ItemDTO> GetAllItems(DateTime dateFrom, DateTime dateTo)
When I try to hit the /items/inventory/all?dateFrom=2015-09-06&dateTo=2015-09-12
route, I get the following error:
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL. The request has found the following matching controller types: ItemAPI.Controllers.ItemsController ItemAPI.Controllers.SingleItemController
So {itemReference}
route is conflicting with all
route. Why is this? I would think that it reserves first all
route and then allows an optional string route.
This is because it can't decide whether "all" is an item reference.
I had a similar issue recently where I had a controller with an "admin" route prefix.
To get round the issue I put a constraint on the parameter to ignore the word "admin".
So in your case you could do the following to ignore the word "all":
[Route("{itemReference:regex(^(?!all))})]