简体   繁体   English

ASP.NET MVC - 向Controller中的RedirectToAction添加查询字符串值

[英]ASP.NET MVC - Add Query String Value to RedirectToAction in Controller

I have an ASP.NET MVC app. 我有一个ASP.NET MVC应用程序。 My app is using T4MVC. 我的应用程序正在使用T4MVC。 In my controller, I need to redirect from one action to another. 在我的控制器中,我需要从一个动作重定向到另一个动作。 When I do this, I want to append a query string value. 当我这样做时,我想追加一个查询字符串值。 I can successfully redirect without the query string value, but I've been unsuccessful applying a query string value. 我可以在没有查询字符串值的情况下成功重定向,但是我在应用查询字符串值时失败了。 My actions look like this: 我的行为如下:

[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Action1()
{                        
   return RedirectToAction(MVC.MyController.Action2().AddRouteValue("id", "5"));
}

[Route("action-2")]
public virtual ActionResult Action2(string input)
{
  ViewBag.Input = input;
  return View(viewModel);
}

The Action2 works fine when I visit ./action-2 . Action2当我访问正常工作./action-2 I can also successfully POST to Action1 . 我也可以成功POST到Action1 But, when the redirect doesn't work. 但是,当重定向不起作用时。 I notice in the address bar the following: 我在地址栏中注意到以下内容:

/MyController/id

Why? 为什么? How do I fix this? 我该如何解决? I just want to redirect back to Action2 but this time with a query string parameter added. 我只是想重定向回Action2但这次添加了一个查询字符串参数。 What am I missing? 我错过了什么?

You need to specify the parameter by the name in the action (in this case " input ") in order for it to work, see below: 您需要通过操作中的名称指定参数(在本例中为“ input ”)才能使其工作,请参见下文:

return redirectToAction(MVC.MyController.Action2().AddRouteValue("input", "5"));

or alternatively: 或者:

return RedirectToAction("Action2", "MyController", new { input = "myInput"});

我尝试以下方式,它对我来说很好。

return RedirectToAction("Index", "CustomBuilder", new { usern = "admin" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM