1

Closed

AddRouteValue() throws an ArgumentException if the route key has already been added.

description

I recently hit a problem when I needed to generate several urls from an ActionResult. They differ by having different route values. The code looks like this:
var url1 = Url.Action(MVC.Home.Index().AddRouteValue("foo", "bar1")); // ok
var url2 = Url.Action(MVC.Home.Index().AddRouteValue("foo", "bar2")); // throws ArgumentException
Is this the correct behaviour, or should AddRouteValue() check to see if the route value is already there and update it if it is?

If we want to make this change I will send a pull request with the changes in.
Closed Jul 5, 2013 at 3:20 PM by davidebbo
It's in 3.7.1, thanks!

comments

davidebbo wrote Jul 4, 2013 at 12:32 PM

The two calls are unrelated, and should not affect each other. When I tried those two lines in a test view, it worked fine. There is probably something else going on with your app.

KevinKuszyk wrote Jul 4, 2013 at 1:42 PM

Hi David,

Thanks for the quick reply. It turns out my example code was wrong. This example actually throws the exception:
var actionResult = MVC.Home.Index();

var url1 = Url.Action(actionResult.AddRouteValue("foo", "bar1")); // ok
var url2 = Url.Action(actionResult.AddRouteValue("foo", "bar2")); // throws ArgumentException
Kevin.

davidebbo wrote Jul 4, 2013 at 5:18 PM

I see. Yes, should just be a matter of changing routeValueDictionary.Add(routeName, routeValue) to routeValueDictionary[routeName] = routeValue in DefaultModelUnbinder. Feel free to send PR! :)

KevinKuszyk wrote Jul 5, 2013 at 8:43 AM

Thanks David. Pull request sent.