小编典典

在同一控制器中具有相同动作名称的GET和POST方法

c#

为什么这不正确?

{
    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();
        }

    }

当“被获取”时,我如何让控制者回答一件事?当“发布”时,我该如何回答?


阅读 387

收藏
2020-05-19

共1个答案

小编典典

由于不能有两个具有相同名称和签名的方法,因此必须使用ActionName属性:

[HttpGet]
public ActionResult Index()
{
  // your code
  return View();
}

[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost()
{
  // your code
  return View();
}

另请参阅“方法如何成为动作”

2020-05-19