How do I pass a collection of values to RedirectToAction for the querystring?

In a controller method in ASP.NET Core, I have a set of name/value pairs in a Dictionary object which I would like to pass to RedirectToAction so that it uses that collection for the querystring part of the URL. However RedirectToAction doesn't accept a collection like that. What is the cleanest way to have it redirect where the querystring parameters can by dynamically defined like this?

I would like to do something like this:

var list = new Dictionary<string, string>();
list.Add("Param1", "A");
list.Add("Param2", "B");
return RedirectToAction("Preview", list);

1 answer

  • answered 2018-03-13 20:29 Stephen Muecke

    The RedirectToAction() method (and all the methods that generate a url) generate the query string values by calling .ToString() on each property of the object passed to the routeValues argument.

    Since your object is Dictionary<> which contains properties Count, Keys, ValuesandComparer`, you would be generating a url that looks like


    The method does not perform recursion (which is probably a good thing, since if the collection contained a lot of items, it would exceed the query string limit and throw an exception.

    In order to bind in your GET method, say

    public ActionResult Preview(Dictionary<string, string> model)

    then your url would need to be


    While you could write a helper method to generate the query string, your generating an ugly url and risk exceeding the query string limit.

    Instead, persist the Dictionary<> (e.g. to a database, Session etc), pass an ID value for it in the RedirectToAction() method, and then retrieve the Dictionary<> again in the GET method.