6

I have to do a redirecttoaction call in asp.net mvc view with varying params, extracted from the referrer page of the view (the status of a grid).

I have (in an hidden field) the content of the query string (sometimes empty, sometimes with 2 parameters and so on), so I have problems to create the route values array.

Are there some helpers, that help me to convert a query string a route values array? Something like:

string querystring ="sortdir=asc&pag=5";
return RedirectToAction( "Index", ConvertToRouteArray(querystring));
tereško
  • 58,060
  • 25
  • 98
  • 150
Luca Morelli
  • 2,530
  • 3
  • 28
  • 45
  • see http://stackoverflow.com/questions/1067200/how-to-add-querystring-values-with-redirecttoaction-method?lq=1 – MVCdragon Nov 14 '14 at 00:00
  • [this has the solution, should help you][1] [1]: http://stackoverflow.com/questions/1067200/how-to-add-querystring-values-with-redirecttoaction-method?lq=1 – MVCdragon Nov 14 '14 at 00:01

3 Answers3

10

To create a generic solution convert your querystring to a Dictionary and at the dictionary to the RouteValueDictionary.

var parsed = HttpUtility.ParseQueryString(temp); 
Dictionary<string,object> querystringDic = parsed.AllKeys
    .ToDictionary(k => k, k => (object)parsed[k]); 

return RedirectToAction("Index", new RouteValueDictionary(querystringDic)); 
Erwin
  • 4,757
  • 3
  • 31
  • 41
  • this is just an example, the problem is that the querystring is variable at every call even in the number of parameters, so i want find a generic solution – Luca Morelli Aug 29 '12 at 14:59
  • I changed my answer to a generic solution. – Erwin Aug 29 '12 at 15:07
  • 1
    forgot the new: return RedirectToAction("Index", new RouteValueDictionary(querystringDic)); – Adam Tuliper Aug 29 '12 at 15:59
  • 1
    thanks, the final working code i wrote is: var parsed = HttpUtility.ParseQueryString(temp); Dictionary querystringDic = parsed.AllKeys.ToDictionary(k => k, k => (object)parsed[k]); return RedirectToAction("DirectUpdate", new RouteValueDictionary(querystringDic)); cause col[k] is undefinded in the sampled i saved the parsed result in a variable, but to be sure the correct RedirectToAction costructor is called i have to be sure i pass a Dictionary, otherwise the generic objects costructor is used – Luca Morelli Aug 31 '12 at 10:28
2

One limitation of using RedirectToAction("actionName", {object with properties}) is that RedirectToAction() has no overload which accepts RedirectToAction(ActionResult(), {object with properties}), so you are forced to use magic strings for the action name (and possibly the controller name).

If you use the T4MVC library, it includes two fluent API helper methods (AddRouteValue(...) and AddRouteValues(...)) which enable you to easily add a single querystring parameter, all properties of an object, or the whole Request.QueryString. You can call these methods either on T4MVC's own ActionResult objects or directly on the RedirectToAction() method. Of course, T4MVC is all about getting rid of magic strings!

As an example: suppose you want to redirect to a login page for a non-authenticated request, and pass the originally requested URL as a query string parameter so you can jump there after successful login. Either of the following syntax examples will work:

return RedirectToAction(MVC.Account.LogOn()).AddRouteValue(@"returnUrl", HttpUtility.UrlEncode(Request.RawUrl));

or

return RedirectToAction(MVC.Account.LogOn().AddRouteValue(@"returnUrl", HttpUtility.UrlEncode(Request.RawUrl)));
Martin_W
  • 1,582
  • 1
  • 19
  • 24
1

A simpler version that skips parsing and reassembling the query string:

string queryString = "?sortdir=asc&pag=5"; //mind the "?"
return Redirect(Url.Action("MyAction") + queryString);
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149