views:

494

answers:

2

Hi there.

I have set up an ASP.NET MVC project, and everything is working great, but I do have one problem with the routing. My Global.asax looks like this:

public static void RegisterRoutes(RouteCollection routes) {
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

So, nothing out of the ordinary. My problem is that when I link to a controller/action/params with an HTML.ActionLink like so:

<%= Html.ActionLink("My link", "SomeAction", "SomeController", new {param="someParam"})%>

it should generate (at least what makes sense in my head) a link such as: http://www.localhost/SomeController/SomeAction/someParam.

But instead it generates a link like this: http://localhost/SomeController/SomeAction?param=someParam

If i manually make a link that links to the expected result (SomeController/SomeAction/someParam) then the right controller and action are called, but the parameter defined in the action method is always null.

Any ideas?

+5  A: 

try adding:

routes.MapRoute(
                    "Default",                                                                                              // Route name
                    "{controller}/{action}/{param}",                                                   // URL with parameters
                    new { controller = "Home", action = "Index", param = "" }  // Parameter defaults
            );
Dan Fish
+4  A: 

I think that link will only use the default route like you expect if the parameter name is id instead of param. You'll have to create a different route if you want to provide some other parameter there.

bdukes
Hmmm, so this means that I have to specify a new routing rule for each new controller action that requires a different set of parameters? This would result a large amount of routing rules for an enterprise application. Isn't there a more dynamic approach than that?
Erik
If this was really dynamic, wouldn't you lose type safety, parameter safety and testability?
bzlm