Avoid Length querystring in URL generated by ActionLink in ASP.NET MVC

How to avoid Length querystring in the URL generated by ActionLink?

Sometimes, while generating the url using @Html.ActionLink, the url doesn’t come as expected. Like following @Html.ActionLink.

@Html.ActionLink("Home using Html.Action", "Index", "Home", new { @class =
"myCSSCalss", style = "color:red;" })

Above method renders following hyperlink


The reason for rendering wrong url is the wrong parameter passed. In the above @Html.ActionLink method, the 3rd parameter is passed as “Home”, however the third parameter expected in this overload method of @Html.ActionLink is routeValues. So the correct parameter for @Html.ActionLink overload method should be

@Html.ActionLink("Home using Html.Action", "Index", new { controller = "Home" }, new
{ @class = "myCSSCalss", style = "color:red;" })

Where 3rd parameter is routeValues and we have passed controller as “Home”. Now this will render the correct url without “Length” querystring.

http://localhost:63087/Home/Index or http://localhost:63087/ as App_Start/RouteConfig.cs has default route with controller as "Home" and action as "Index" defined by default.

 Views: 21692 | Post Order: 75

Write for us

Hosting Recommendations