Search code examples
asp.net-mvc-5attributeroutingasp.net-mvc-controllerasp.net-mvc-5.1

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL


...guess I'm the first to ask about this one?

Say you have the following routes, each declared on a different controller:

[HttpGet, Route("sign-up/register", Order = 1)]
[HttpGet, Route("sign-up/{ticket}", Order = 2)]

... you could do this in MVC 5.0 with the same code except for the Order parameter. But after upgrading to MVC 5.1, you get the exception message in the question title:

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

So the new RouteAttribute.Order property is only controller-level? I know in AttributeRouting.NET you can do SitePrecedence too. Is the only way to have routes like the above when all actions are in the same controller?

Update

Sorry, I should have mentioned these routes are on MVC controllers, not WebAPI. I am not sure how this affects ApiControllers.


Solution

  • In case of Attribute routing, Web API tries to find all the controllers which match a request. If it sees that multiple controllers are able to handle this, then it throws an exception as it considers this to be possibly an user error. This route probing is different from regular routing where the first match wins.

    As a workaround, if you have these two actions within the same controller, then Web API honors the route precedence and you should see your scenario working.