![](/img/trans.png)
[英]How to add querystring values with RedirectToAction method that is pointing to a new Area?
[英]how to add querystring values with RedirectToAction method?
在 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 Keys 不能以数字开头。 从404
更改为FileNotFound
解决了我的问题,即mysite.com?FileNotFound=nonExistentFile.txt
。
如果你已经有来自某处的查询字符串,你可以跳过解析和重新组装查询字符串,只需将Redirect
与Url.Action
一起使用:
string queryString ="?param1=xxx&page=5";
return Redirect(Url.Action("MyAction") + queryString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.