0
votes

I'm debugging a route matching issue in ASP.NET MVC. The URL is defined like this: {foo}/Somewhere/{controller}/{action}/{id}. Each segment has a default value set up on the route.

When I try to browse http://hostname/bar/Somewhere the route matches. If I try http://hostname/Somewhere (thus, omitting foo segment), the route doesn't match.

I've installed routedebugger from Phil Haack and I can see that my defaults are correctly set up (controller = Home, action = Index, id = UrlParameter.Optional, foo = bar), but the route still does not match and I get a 404.

I tried to modify the route to have no "optionnal" segment before the "static" segment, like this: Somewhere/{controller}/{action}/{id}/{foo}. With this route, both url (http://hostname/Somewhere/bar and http://hostname/Somewhere) are working as expected.

Is there a limitation (or a bug) in MVC that I can't use an "optionnal" segement before an hard-coded one?

Thanks for your help, Fabian

1

1 Answers

0
votes

This is only logical. Your foo placeholder cannot be suddenly empty or omitted. And if you ask my opinion, I must say it would not be a good thing to omit. These route, in decreasing order of size, must map to your app's modules and functionality.

Basically, as this question also answers, you cannot have optional segment in the middle of the route.