0

I have two routes in my application, each in a different Controller, that look like this:

[Route("forgot-password", Order = 1)]
[Route("{variable}", Order = 2)]

When I run the application I get the exception:

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

Remember these actions are in different Controllers. The Order attribute doesn't seem to work across Controllers!.

How can I get this scenario to work in asp.net mvc routing? I want to use attribute based routing and I don't want to change my urls.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
BowserKingKoopa
  • 2,701
  • 3
  • 33
  • 40

2 Answers2

0

The mechanism behind the error is explained here (I guess you are using Web API). Shortly put, precedence is honored only inside a controller.

Also, it is unclear for me why you want such a generic route like:

[Route("{variable}")]
Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • I'm using MVC, not WebApi. What if you have a website selling cars? mywebsite.com/ford and mywebsite.com/toyota seem like sensible urls. I understand the mechanism behind the error but I'm in utter disbelief that asp.net routing can't handle this scenario. Other attribute based routing libraries just let you specify an Order parameter to resolve conflicts. – BowserKingKoopa Jan 12 '16 at 20:20
  • @BowserKingKoopa - This scenario is supported. Route attributes are not the most flexible mechanism when using .NET Routing. Instead, you can [subclass `RouteBase`](http://stackoverflow.com/questions/31934144/multiple-levels-in-mvc-custom-routing/31958586#31958586) to handle more complex scenarios such as this. You can even route based on other parts of the context rather than the URL exclusively (cookies, session, or form values, for example). Another alternative (simpler) would just be to put your routes in the `RouteConfig.cs` file using `MapRoute`. – NightOwl888 Jan 12 '16 at 21:14
  • Is this a complex scenario? Every other attribute based routing library I've used supports this easily. – BowserKingKoopa Jan 12 '16 at 21:32
0

The problem is that all routes in your application have been stored together. Even if it is located in different controllers, it is the same type, so they can "see" each other. In your case "forgot-password" and "{variable}" have the same format, that's why error about multiple routes has been displayed. As @NightOwl888 said, you may use RouteConfig to create a routes, but in that case you have to change your routing values.

Khazratbek
  • 1,656
  • 2
  • 10
  • 17
  • The Order value should resolve the conflict. The idea that the Order value doesn't work across Controllers is too stupid to be true and I'm still in denial about that. – BowserKingKoopa Jan 13 '16 at 01:49