ASP.NET MVC - 使用 RedirectToAction 传递当前 GET 参数
我正在寻找一种在传递当前请求的 GET 参数时使用 RedirectToAction 的方法。
所以去之后: http://mydomain.com/MyController/MyRedirectAction?somevalue=1234
然后我想要使用重定向来重定向和保留某个值,而无需显式构建路由字典并显式设置某个值
public ActionResult MyRedirectAction()
{
if (SomeCondition)
RedirectToAction("MyAction", "Home");
}
重定向操作可以使用某个值(如果可用):
public ActionResult MyAction()
{
string someValue = Request.QueryString["somevalue"];
}
是否有一种干净的方法可以做到这一点?
I'm looking for a way to use RedirectToAction while passing along the current request's GET parameters.
So upon going to:
http://mydomain.com/MyController/MyRedirectAction?somevalue=1234
I would then want to redirect and persist somevalue with the a redirect without having to explicitly build a route dictionary and explicitly setting somevalue
public ActionResult MyRedirectAction()
{
if (SomeCondition)
RedirectToAction("MyAction", "Home");
}
The redirected action could then use somevalue if available:
public ActionResult MyAction()
{
string someValue = Request.QueryString["somevalue"];
}
Is there a clean way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自定义操作结果可以完成这项工作:
然后:
A custom action result could do the job:
and then:
如果您使用
RedirectToRoute
传递参数,它们将在 URL 中显示为查询字符串。我不是 ASP .NET MVC 框架方面的专家,但实现此目的的一种方法是将数据放入 TempData 字典中。
您重定向到的操作应该知道检查此数据,当然还要处理它不存在的情况。该数据不会在重定向之后继续存在——它只适用于一个请求。
如果您不喜欢对键使用字符串文字,则可以为 TempData 键创建常量。
If you pass the parameters, using
RedirectToRoute
, they will appear as a query string in the URL.I'm not an expert on the ASP .NET MVC framework, but one way to achieve this is to put your data into the TempData dictionary.
The action to which you are redirecting should know to check for this data, and of course handle the case where it does not exist. That data won't live beyond the redirection -- it is only good for one request.
You can create constants for your TempData keys if you don't like using string literals for your keys.