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.)
Case 1
new { controller="Home", action="Index" });
Case 2
new { controller="Home", action="" });
- http://mywebapp.net/
Error: The RouteData must contain an item named 'action' with a non-empty string value. - http://mywebapp.net/Home/
Error: The RouteData must contain an item named 'action' with a non-empty string value. - http://mywebapp.net/Home/Index/
The above error with 2nd URL is because the routing system couldn't find a default action name.
Case 3
new { controller="", action="" });
- http://mywebapp.net/
Error: Value cannot be null or empty. Parameter name: controllerName - http://mywebapp.net/Home/
Error: The RouteData must contain an item named 'action' with a non-empty string value. - http://mywebapp.net/Home/Index/
The above error with 1st and 2nd URLs is because the routing system couldn't find default controller and action names respectively.
Case 4
new { controller="", action="Index" });
- http://mywebapp.net/
Error: Value cannot be null or empty. Parameter name: controllerName - http://mywebapp.net/Home/ (How and why is this accessible?)
- http://mywebapp.net/Home/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?