Подтвердить что ты не робот

С# ASP.NET MVC: выяснить, были ли GET или POST выведено на действие контроллера

Как узнать, попало ли GET или POST в действие моего контроллера ASP.NET MVC?

4b9b3361

Ответ 1

Вы можете проверить Request.HttpMethod на это.

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

Ответ 2

Вы можете отделить свои методы управления:

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

Ответ 3

Вы также можете использовать методы ActionResults для Get и Post отдельно, как показано ниже:

[HttpGet]
public ActionResult Operation()
{

   return View(...)
}


[HttpPost]
public ActionResult Operation(SomeModel model)
{

   return View(...);
}