NET MVC 中 GET 和 POST 到同一个控制器操作

我希望有一个单一的动作响应两个获得以及发布

[HttpGet]
[HttpPost]
public ActionResult SignIn()

看起来没什么用,有什么建议吗?

66785 次浏览
[HttpGet]
public ActionResult SignIn()
{
}


[HttpPost]
public ActionResult SignIn(FormCollection form)
{
}

Actions respond to both GETs and POSTs by default, so you don't have to specify anything:

public ActionResult SignIn()
{
//how'd we get here?
string method = HttpContext.Request.HttpMethod;
return View();
}

Depending on your need you could still perform different logic depending on the HttpMethod by operating on the HttpContext.Request.HttpMethod value.

This is possible using the AcceptVerbs attribute. Its a bit more verbose but more flexible.

[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
public ActionResult SignIn()
{
}

More on msdn.