C# 参数不起作用的重定向操作

C# 参数不起作用的重定向操作,c#,asp.net-mvc,redirecttoaction,C#,Asp.net Mvc,Redirecttoaction,我正在尝试重定向到同一控制器中的另一个操作 这个动作叫做索引 [HttpGet] public ActionResult Search(string city) { return RedirectToAction("Index", "Rentals", new { CityName = city }); } 这是索引操作 [HttpPost] public ActionResult Index(String CityName) { } 我遗漏了什么吗?您正在尝试重定向正在搜索匹

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

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

}
这是索引操作

[HttpPost]
public ActionResult Index(String CityName)
{


}

我遗漏了什么吗?

您正在尝试重定向正在搜索匹配操作的操作,但在本例中没有get操作,因此您必须添加get方法以接受重定向。如果需要,可以在方法内部检查HTTPGET或POST

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


}

[HttpPost]由于这两个操作在同一个控制器中,您可以直接从
Search
调用
Index
方法,如下所示:

return Index(city);

不一定要使用
重定向到操作
方法。

请将
HttpPost
更改为
HttpGet

[HttpGet]
public ActionResult Index(String CityName)
{


}

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

如果将属性从HttpPost更改为HttpGet,会发生什么情况?如果您看起来像黑客一样