3
votes

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" });
  1. http://mywebapp.net/
  2. http://mywebapp.net/Home/
  3. http://mywebapp.net/Home/Index/

Case 2

new { controller="Home", action="" });
  1. http://mywebapp.net/
    Error: The RouteData must contain an item named 'action' with a non-empty string value.
  2. http://mywebapp.net/Home/
    Error: The RouteData must contain an item named 'action' with a non-empty string value.
  3. 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="" });
  1. http://mywebapp.net/
    Error: Value cannot be null or empty. Parameter name: controllerName
  2. http://mywebapp.net/Home/
    Error: The RouteData must contain an item named 'action' with a non-empty string value.
  3. 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" });
  1. http://mywebapp.net/
    Error: Value cannot be null or empty. Parameter name: controllerName
  2. http://mywebapp.net/Home/ (How and why is this accessible?)
  3. 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?

1
Case 4.1 works because you've specified the controller and there's a default action configured. I'm more concerned that Case 2.1 works, since there's no default action configured. - Richard Szalay
@RichardSzalay: Thanks for pointing it out. I think I overlooked it. Case 2.1 also does not work. I updated the question. - Animesh

1 Answers

4
votes

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.