how to add querystring values with RedirectToActio

2019-01-13 19:49发布

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?

3条回答
等我变得足够好
2楼-- · 2019-01-13 20:08

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".

查看更多
趁早两清
3楼-- · 2019-01-13 20:18

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

查看更多
We Are One
4楼-- · 2019-01-13 20:26

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.

查看更多
登录 后发表回答