在 asp.net mvc 中,我使用以下代码:
RedirectToAction("myActionName");
我想通过查询字符串传递一些值,我该怎么做?
在 asp.net mvc 中,我使用以下代码:
RedirectToAction("myActionName");
我想通过查询字符串传递一些值,我该怎么做?
传递的任何不属于路由的值都将用作查询字符串参数:
return this.RedirectToAction
("myActionName", new { value1 = "queryStringValue1" });
将返回:
/controller/myActionName?value1=queryStringValue1
假设没有名为“value1”的路由参数。
对于像我这样希望将 CURRENT 查询字符串值添加到 RedirectToAction 的人来说,这是解决方案:
var routeValuesDictionary = new RouteValueDictionary();
Request.QueryString.AllKeys.ForEach(key => routeValuesDictionary.Add(key, Request.QueryString[key]));
routeValuesDictionary.Add("AnotherFixedParm", "true");
RedirectToAction("ActionName", "Controller", routeValuesDictionary);
如您所见,解决方案是使用RouteValueDictionary对象
还可以考虑使用T4MVC,它具有扩展方法AddRouteValue()
和AddRouteValues()
(如在这个问题上看到的在 redirecttoaction 中设置查询字符串)。
不要犯我犯的同样的错误。我正在处理 404 错误并想404=filename
在查询字符串中重定向,即mysite.com?404=nonExistentFile.txt
.
QueryString 键不能以数字开头。更改404
为FileNotFound
解决了我的问题,即mysite.com?FileNotFound=nonExistentFile.txt
.