asp.net mvc Html.ActionLink() keeping route value I don't want

codette picture codette · Apr 23, 2009 · Viewed 35.8k times · Source

I have the following ActionLink in my view

<%= Html.ActionLink("LinkText", "Action", "Controller"); %>

and it creates the following URL http://mywebsite.com/Controller/Action

Say I add an ID at the end like so: http://mywebsite.com/Controller/Action/53 and navigate to the page. On this page I have the markup I specified above. Now when I look at the URL it creates it looks like this:

http://mywebsite.com/Controller/Action/53 (notice the addition of the ID)

But I want it to remove the ID and look like it did originally, like this http://mywebsite.com/Controller/Action (notice no ID here)

Any ideas how I can fix this? I don't want to use hard coded URLs since my controller/actions may change.

Answer

codette picture codette · Apr 23, 2009

The solution is to specify my own route values (the third parameter below)

<%= Html.ActionLink("LinkText", "Action", "Controller", 
    new { id=string.Empty }, null) %>