1

In my mvc3 razor project my I have action link

@Html.ActionLink("ActionLinkName","Count","Home",new{id = 3})

This generates localhost/Home/Count/3

but I want it to create localhost/Home/Count?id=3

What changes in route should I make ?

4
  • It's the 'id' parameter in your default route that's telling mvc to create the url like that. You could use something other than 'id', or change the route to expect a differently named parameter. Commented Jun 17, 2014 at 13:56
  • 2
    Just out of curiosity: WHY would you want that? Commented Jun 17, 2014 at 14:01
  • my client wants that kind of link. Commented Jun 17, 2014 at 14:16
  • well, whatever the client wants... but that seems backwards to me. MVC makes it easy for URLs to be more user and SEO friendly, and query strings just aren't. Commented Jun 17, 2014 at 14:17

3 Answers 3

2

This is because the default route that's used with new MVC projects includes an {id} segment. Removing that segment from your default route will make your existing code produce a query string.

Change this:

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

To this:

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

Comments

1

Since the default (fallback) route that Asp.Net registers is including an {id} parameter in the url pattern:

// Default
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Your generated url adheres to this fallback pattern ({controller}/{action}/{id}) and appends your id using /{id}.

You'll need to register a custom route before the default route to exclude the {id}:

routes.MapRoute(
    name: "Count",
    url: "Home/Count",
    defaults: new { controller = "Home", action = "Count" }
);

You can also try to remove the {id} from the default pattern (if it won't affect other actions) or change your id parameter in the Count action to a different name.

Comments

0

remove {id} from default config

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional }
    );
}

TO

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}",
        defaults: new { 
            controller = "Home", 
            action = "Index" }
    );
}

Comments

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.