views:

67

answers:

3

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 ?

A: 
[HttpGet]
public ActionResult SignIn()
{
}

[HttpPost]
public ActionResult SignIn(FormCollection form)
{
}
Neil
That is not what I am looking for, thats the default MVC implementation of having separate methods for GET and POST via function overloading. I am not new to MVC, I am trying to have the GET action also respond to certain POST events in addition to the standard POST action for the form collection.
Cranialsurge
Then you need to follow Kurts' answer. No attribute will handle both. If you are attempting to have POST requests going to different actions, that is not possible. Your action will have to perform the switching you are looking for.
Jeremy B.
+2  A: 

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.

Kurt Schindler
A: 

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.

EvilRyry