91

In asp.net mvc, I am using this code:

RedirectToAction("myActionName");

I want to pass some values via the querystring, how do I do that?

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
mrblah
  • 99,669
  • 140
  • 310
  • 420

6 Answers6

185

Any values that are passed that aren't part of the route will be used as querystring parameters:

return this.RedirectToAction
  ("myActionName", new { value1 = "queryStringValue1" });

Would return:

/controller/myActionName?value1=queryStringValue1

Assuming there's no route parameter named "value1".

Talljoe
  • 14,593
  • 4
  • 43
  • 39
  • Agree, but action parameter with name "value1" could be present. Why not? – Alexander Prokofyev Jul 01 '09 at 04:20
  • 7
    I think the answer meant "assuming there's no route parameter named 'value1'". Otherwise the value would go into the route parameter's place in the generated URL, e.g. {controller}/{action}/{value1} would become /controller/myActionName/queryStringValue1 rather than /controller/myActionName?value1=queryStringValue1. – Levi Jul 01 '09 at 04:30
  • Levi is correct. I've fixed the answer to clarify what I meant. – Talljoe Jul 01 '09 at 04:51
12

For people like me who were looking to add the CURRENT querystring values to the RedirectToAction, this is the solution:

var routeValuesDictionary = new RouteValueDictionary();
Request.QueryString.AllKeys.ForEach(key => routeValuesDictionary.Add(key, Request.QueryString[key]));
routeValuesDictionary.Add("AnotherFixedParm", "true");
RedirectToAction("ActionName", "Controller", routeValuesDictionary);

The solution as you can see is to use the RouteValueDictionary object

4

Also consider using T4MVC, which has the extension methods AddRouteValue() and AddRouteValues() (as seen on this question on setting query string in redirecttoaction).

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Martin_W
  • 1,582
  • 1
  • 19
  • 24
3

Do not make the same mistake I was making. I was handling 404 errors and wanted to redirect with 404=filename in the querystring, i.e. mysite.com?404=nonExistentFile.txt.

QueryString Keys cannot begin with numbers. Changing from 404 to FileNotFound solved my issue, i.e. mysite.com?FileNotFound=nonExistentFile.txt.

Nick
  • 882
  • 2
  • 9
  • 31
  • 1
    This isn't [generally true](https://stackoverflow.com/a/12465767/264628). It's also not true specifically for .NET MVC (at least in v5). Just remember that the keys are strings even if it consists only of numerals. You would access a numeric key as follows: `HttpContext.Request.QueryString["404"]`. – BrianS Dec 29 '17 at 02:40
0

If you already have a query string from somewhere, you can skip parsing and reassembling the query string and just use Redirect with Url.Action:

string queryString ="?param1=xxx&page=5";
return Redirect(Url.Action("MyAction") + queryString);
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
0

Redirect("myAction/" + Request.QueryString);

  • Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? **If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient.** Can you kindly [edit] your answer to offer an explanation? – Jeremy Caney Aug 30 '23 at 00:27