开发者

GET and POST to same Controller Action in ASP.NET MVC

I'd like开发者_开发问答 to have a single action respond to both Gets as well as Posts. I tried the following

[HttpGet]
[HttpPost]
public ActionResult SignIn()

That didn't seem to work. Any suggestions ?


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.


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.


[HttpGet]
public ActionResult SignIn()
{
}

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜