views:

33

answers:

2

What is the problem below?

    routes.MapRoute(
         "Default2", // Route name
         "{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = "test" } // Parameter defaults
     );

    routes.MapRoute(
         "Default1", // Route name
         "{controller}/{action}/{name}", // URL with parameters
         new { controller = "Home", action = "Report", name = "" } // Parameter defaults
     );

When I navigate to /home/index "id" parameter takes the default value of "test" but when I navigate to home/report the name parameter is null.

In short, if the route definition is the first in the route table, then the parameter takes its default value. But the others below don't.

A: 

These two routes {controller}/{action}/{id} and {controller}/{action}/{name} are ambiguous. It cannot distinguish between /home/index/id and /home/report/abc, it is always the first route in the route definition which will be caught because in the second case it thinks that id = "abc".

Darin Dimitrov
So what should I do to assign default values to parameters? Should I create different routes for every controller by changing the {controller} parameter to real controller name? Any other way?
yang
Changing the {controller} won't work. A route must be defined for every action.
yang
A: 

Use Phil Haack Routes debugger.. to get more clear view how your routes are reacting on diferent paths.

download

Florim Maxhuni