在同一控制器中具有相同操作名称的 GET 和 POST 方法 [英] GET and POST methods with the same Action name in the same Controller
本文介绍了在同一控制器中具有相同操作名称的 GET 和 POST 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这是不正确的?
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
[HttpPost]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
}
如何让控制器在获取"和发布"时回答一件事?
How can I have a controlller thas answer one thing when is "getted" and one when is "posted"?
推荐答案
由于不能有两个具有相同名称和签名的方法,因此必须使用 ActionName
属性:
Since you cannot have two methods with the same name and signature you have to use the ActionName
attribute:
[HttpGet]
public ActionResult Index()
{
// your code
return View();
}
[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost()
{
// your code
return View();
}
另请参阅"方法如何变成一个动作"
这篇关于在同一控制器中具有相同操作名称的 GET 和 POST 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文