【问题标题】:RedirectToAction with a parameter not working带有参数的 RedirectToAction 不起作用
【发布时间】:2014-09-16 20:17:47
【问题描述】:

我正在尝试重定向到同一控制器中的另一个操作 动作称为索引

[HttpGet]
public ActionResult Search(string city)
{
    return RedirectToAction("Index", "Rentals", new { CityName = city });

}

这是索引操作

[HttpPost]
public ActionResult Index(String CityName)
{


}

我错过了什么吗?

【问题讨论】:

  • 如果将属性从 HttpPost 更改为 HttpGet 会发生什么?
  • 如果你正在寻找像here这样的黑客攻击

标签: c# asp.net-mvc redirecttoaction


【解决方案1】:

您正在尝试重定向正在搜索匹配操作的操作,但在这种情况下没有 get 操作,因此您必须添加一个 get 方法来接受重定向。如果需要,可以检查方法内的 HTTPGET 或 POST

[HttpPost]<---- Remove this 
public ActionResult Index(String CityName)
{


}

【讨论】:

    【解决方案2】:

    请将HttpPost改为HttpGet

    [HttpGet]
    public ActionResult Index(String CityName)
    {
    
    
    }
    

    因为无论何时调用 Action,都会首先调用 GET 方法。

    【讨论】:

      【解决方案3】:

      由于这两个动作在同一个控制器中,您可以像这样直接从Search 调用Index 方法:

      return Index(city);
      

      不一定要使用RedirectToAction 方法。

      【讨论】:

        猜你喜欢
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        相关资源
        最近更新 更多