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

asp.net Mvcasp.net Mvc-2

asp.net Mvc Problem Overview


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 ?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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.

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

[HttpGet]
public ActionResult SignIn()
{
}

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCranialsurgeView Question on Stackoverflow
Solution 1 - asp.net MvcRyan BairView Answer on Stackoverflow
Solution 2 - asp.net MvcKurt SchindlerView Answer on Stackoverflow
Solution 3 - asp.net MvcNeil OutlerView Answer on Stackoverflow