5

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?

2 Answers 2

5

try adding:

routes.MapRoute(
                    "Default",                                                                                              // Route name
                    "{controller}/{action}/{param}",                                                   // URL with parameters
                    new { controller = "Home", action = "Index", param = "" }  // Parameter defaults
            );
Sign up to request clarification or add additional context in comments.

Comments

4

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.

2 Comments

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?
If this was really dynamic, wouldn't you lose type safety, parameter safety and testability?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.