Having some trouble with some routes. I don’t fully understand the MVC routing system so bear with me.
I’ve got two controllers, Products and Home (with more to come!).
I want to have the views within the Home controller accessible without having to type Home in the url. Essentially I want to turn http://www.example.com/home/about into http://www.example.com/about, however I still want to preserve the http://www.example.com/products.
Here’s what I have so far.
routes.MapRoute( "Home", "{action}", new { controller = "Home" } );
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "home", action = "index", id = UrlParameter.Optional }
);
Now depending on which one is first I can get either one or the other to work, but not both.
I think what you might be looking for is something that that the author of the code below has termed a Root Controller. I have used this myself on a couple sites, and it really makes for nice URLS, while not requiring you to create more controllers that you’d like to, or end up with duplicate URLs.
This route is in Global.asax:
With this defined elsewhere:
The RootActionContraint alows you to still have other routes, and prevents the RootController actions from hiding any controllers.
You also need to create a controller called Root. This is not a complete implementation. Read the original article here