Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Routing Html.ActionLink creates URLs with ?id=1 instead of /id

When I use Html.ActionLink() the URL created is not in the desired format:

Html.ActionLink(Model.ProductCode, "Update", new { id = Model.ProductId })

Makes this URL

/Update?id=1

When I want to have this URL:

/Update/1

What routing options create the 2nd URL? This is our preferred URL style.

Both URLs work and the correct page is displayed - however we want to only use /id

In Global.asax the MVC default route handles both URLs

routes.MapRoute(
    "Default",                                               // Route name
    "{controller}/{action}/{id}",                            // URL with parameters
    new { controller = "Home", action = "Index", id = "" }); // Parameter defaults
like image 854
JK. Avatar asked Jan 04 '12 02:01

JK.


1 Answers

I can replicate the issue by having a route about my default route that still matches the general pattern. Example:

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

When places above my default route, I get the ?id=1 in my URL. Can you confirm that this ActionLink is not matching any routes above the route that you are expecting it to match?

EDIT: The below does not impact the URL

However, it could still be advantageous to use the UrlParameter.Optional in other scenarios. Leaving for prosperity unless mob rule says otherwise.

new UrlParameter.Optional value. If you set the default value for a URL parameter to this special value, MVC makes sure to remove that key from the route value dictionary so that it doesn’t exist.

I think you need to adjust your route slightly. Change id = "" to id = UrlParameter.Optional

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

This is what we use for the default route and the behavior that you are looking for is how our applications behave.

like image 152
Tommy Avatar answered Oct 27 '22 00:10

Tommy