C#ASP.NET MVC:确定是否在控制器操作上调用了GET或POST

时间:2009-07-23 03:45:06

标签: asp.net-mvc

如何确定GET或POST是否命中了我的ASP.NET MVC控制器操作?

3 个答案:

答案 0 :(得分:29)

您可以检查Request.HttpMethod

if (Request.HttpMethod == "POST") {
    //the controller was hit with POST
}
else {
    //etc.
}

答案 1 :(得分:11)

您可以分离控制器方法:

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Operation()
{
   // insert here the GET logic
   return SomeView(...)
}


[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Operation(SomeModel model)
{
   // insert here the POST logic
   return SomeView(...);
}

答案 2 :(得分:1)

您还可以单独使用ActionResults For Get和Post方法,如下所示:

[HttpGet]
public ActionResult Operation()
{

   return View(...)
}


[HttpPost]
public ActionResult Operation(SomeModel model)
{

   return View(...);
}