The question is similar to asp.net mvc Html.ActionLink() keeping route value I don't want, but with a twist that makes it more complex.
Starting from a default n
To solve it in this case is quite easy if yo don't care how it's solved.
Change the third link to:
@Url.RouteUrl("R2")
which gives me:
Why it happends in the first case is something I have yet to figure out. I have been bitten by this too but mostly when it comes to forms.
UPDATE 1:
I was digging around the MVC source and created a few tests that reproduced this problem. The problem seems to be when
RouteCollection.GetVirtualPath(requestContext, correctedValues);
is run. It creates a querystring with the old id. Exactly why I couldn't tell as that class isn't located in the MVC source.
This is probably not the answer you're looking for, but using Url.Route instead of Url.Action seems to work:
@Url.RouteUrl("r2")
This generates /Home/Index
Update
Phil Haack recommendeds generating URLs using the route name:
This might seem like a big problem, but the fix is actually very simple. Use names for all your routes and always use the route name when generating URLs. Most of the time, letting routing sort out which route you want to use to generate an URL is really leaving it to chance. When generating an URL, you generally know exactly which route you want to link to, so you might as well specify it by name.
I just tested this and it seems to work ok.
Url.Action("Index", new { id = UrlParameter.Optional })
generates
/Home/Index
Because r2 does not define the id parameter to be a part of the URL? If you change it to {controller}/{action}/{id}, it will be fine. You'll see the same behaviour if you change r2 to not include action: action will then also becoe a query variable.
The fact that you explicitly set it to "" means that routing will take it into account anyway. Just don't add id and you'll be fine when generating a link.