Asp.net mvc C#ASP.NET MVC:查明在控制器操作上是否调用了GET或POST

Asp.net mvc C#ASP.NET MVC:查明在控制器操作上是否调用了GET或POST,asp.net-mvc,Asp.net Mvc,如何确定GET或POST是否命中我的ASP.NET MVC控制器操作?您可以检查Request.HttpMethod if (Request.HttpMethod == "POST") { //the controller was hit with POST } else { //etc. } 您可以分离控制器方法: [AcceptVerbs(HttpVerbs.Get)] public ViewResult Operation() { // insert here th

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

您可以检查
Request.HttpMethod

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

您可以分离控制器方法:

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

您还可以分别使用Get和Post方法的ActionResults,如下所示:

[HttpGet]
public ActionResult Operation()
{

   return View(...)
}


[HttpPost]
public ActionResult Operation(SomeModel model)
{

   return View(...);
}