I was reading up on URLs and Routes chapter in Pro ASP.NET MVC 3 and tried to see what happens when the object defaults contain empty values. This is the route I have at the moment.
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller="Home", action="Index" });
Following are observations I made with possible combinations of values of object defaults. (In each case, URLs in BOLD are not accessible.)
new { controller="Home", action="Index" });
new { controller="Home", action="" });
The above error with 2nd URL is because the routing system couldn't find a default action name.
new { controller="", action="" });
The above error with 1st and 2nd URLs is because the routing system couldn't find default controller and action names respectively.
new { controller="", action="Index" });
When both controller and action properties are empty in Case 3, I received errors as expected. So in Case 4, how is 2nd URL http://mywebapp.net/Home/ accessible when I have an empty value for controller property?
Is it as simple as the 2nd URL being accessible because a HomeController is already defined and the routing system by found it by convention or is there some explanation for this behavior? Can this be modified to let the 2nd URL be inaccessible or does this violate the convention-over-configuration principle?
Home and Index are not hardcoded conventions, unless you specify them as defaults the runtime won't attempt to find them.
Case 2.1 does not work because the URL did not contain a value for the action parameter, and there's no action default.
Case 4.1 does not work because the URL did not contain a value for the controller parameter, and there's no controller default.
I recommend this post if you want more details about how routing works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With