如何与RedirectToAction方法添加查询字符串值?(how to add querystr

2019-06-24 14:26发布

在asp.net的MVC,我使用这个代码:

RedirectToAction("myActionName");

我想通过查询字符串传递一些值,我该怎么做呢?

Answer 1:

所传递不属于路线的一部分的任何值将被用作查询字符串参数:

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

将返回:

/controller/myActionName?value1=queryStringValue1

假设有一个名为“VALUE1”无路由参数。



Answer 2:

也可以考虑使用T4MVC ,它具有扩展方法AddRouteValue()AddRouteValues()作为上看到关于这个问题的redirecttoaction设置查询字符串 )。



Answer 3:

不要让我犯同样的错误。 我正在处理404错误,并希望与重定向404=filename中的查询字符串,即mysite.com?404=nonExistentFile.txt

查询字符串键不能以数字开头。 改变从404FileNotFound解决了我的问题,即mysite.com?FileNotFound=nonExistentFile.txt



文章来源: how to add querystring values with RedirectToAction method?