My goal is to remove “home” from any actions in that controller (see bold).
site.com/home/about
site.com/about
site.com/home/contact
site.com/contact
I created the following custom route that sits above the generic base route:
// Used to hide 'home' in the url
routes.MapRoute(
"Home", // Route name
"{action}", // URL with parameters
new { controller = "home", action= "index"} // Parameter defaults
);
This almost does what I want. I now get site.com/about, site.com/contact, etc. However, I cannot use index for my other controllers.
site.com/person/create -> works like a charm.
site.com/person/ -> no good.
How can I fix this? Thanks.
These two routes should work as expected:
Since every application has a predefined set of controllers you can put all except home in the upper constraint and it will work. But if you create a new controller, remember to put it in as well. I’ve put in person for
PersonControllerwhich you obviously have and also added admin forAdminControlleryou probably don’t have, but I needed to put in something to show you the pattern of adding your controllers.If you’re willing to play around with regular expressions, then you could maybe come up with a solution that excludes home instead of includes all except home the way that upper route definitions suggest.
A revised negative constraint
I’ve checked MVC code and indeed you can define a future proof constraint on the first route definition this way:
Why should this work? Because the line on
ProcessConstraintmethod has these two lines at the end: