Search code examples
asp.net-mvcattributerouting

MVC Route Attribute error on two different routes


I'm getting an attribute routing error with MVC 5.2. The error is

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

The routes in question are

[Route("{classname}/{id:int}")]
[Route("Edit/{id:int}")]

The url /Edit/123 throws the error, while the url /someword/123 does not throw the error

Given that Edit/123 is more specific than someword/123 why would it throw an error on /Edit/123?

Thanks,

john


Solution

  • The routing framework doesn't make judgments about what route you probably intended (there's actually a route with Edit in it, so obviously I want that one). All it sees is that it has two routes which match the URL it has, and it throws its hands up.

    Something like the following should fix the ambiguity, as long as you never need "Edit" as a value for classname:

    [Route("{classname:regex(^(?!Edit)$)}/{id:int}")]