Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

try adding:

routes.MapRoute(
                    "Default",                                                                                              // Route name
                    "{controller}/{action}/{param}",                                                   // URL with parameters
                    new { controller = "Home", action = "Index", param = "" }  // Parameter defaults
            );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top