bad links generated when using action overloading with [FromRoute] and [RequireRouteParameters] #292

Imported from the CodePlex archive for reference purposes. Support for MvcCodeRouting has ended.

Reported on

Given the following action methods in a single controller:

[RequireRouteParameters] public ActionResult Index()

and [RequireRouteParameters] public ActionResult Index([FromRoute] int someParam)

A link the the second, more specific action cannot be generated using Html.ActionLink. Even if you provide the “someParam” parameter as a route value, it will generate a link to the parameterless Index method, with the someParam value appended to the querystring.

I have attached a sample project to demonstrate the issue. Just start it up and you will see an explanation on the default action/view. There are two links there, and both point to the same parameterless Index method no matter how you adjust the call to Html.ActionLink. The problem also exists with other url-generating methods (like Url.Action).

(Sorry for all the issues lately; just testing this as thoroughly as I can.)

Commented on link

Attached is a patch that appears to fix the issue, but I am not 100% sure it won’t cause others. The patch should be applied from the root of the svn repo (not within the trunk folder, but the actual repo root).

Because of the way MVC merges the current request’s route values in when generating urls (I HATE that) you always have to set the someParam route value to “” to generate a link to the default Index method, but that’s just the way MVC is. So with this patch to MvcCodeRouting, the sample project must be changed to use this line, and everything will work as expected:

@Html.ActionLink(“Index with no parameters”, “Index”, new { someParam = “” })

Commented on link

Don’t be sorry, thank you for all the testing you’ve done, it would have taken me months to find these issues myself.

This issue exists only for actions in the root controller, because I’m putting the “{action}” route at the top. For other controllers the routes with most parameters come first, precisely to avoid the url generation issue.

I’m not sure what your patch does, but my solution would be to order the routes for the root controller the same ways as it’s done for the rest.

About MVC copying the current route values, this would only be an issue if the current request has the someParam value. I other cases you shouldn’t need the empty string.

Thanks again, we are getting close to 1.0 :-)