[ACCEPTED]-post and get with same method signature-asp-net-mvc-1

Accepted answer
Score: 128

Rename the second method to something else 4 like "Friends_Post" and then you can add 3 [ActionName("Friends")] attribute to the second one. So the requests 2 to the Friend action with POST as request 1 type, will be handled by that action.

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[ActionName("Friends")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends_Post()
{
    // do some stuff
    return View();
}
Score: 25

If you truly only want one routine to handle 4 both verbs, try this:

[AcceptVerbs("Get", "Post")]
public ActionResult ActionName(string param1, ...)
{
//Fun stuff goes here.
}

One potential caveat: I'm 3 using MVC release 2. Not sure if this was 2 supported in MVC 1. The Intellisense documentation 1 for AcceptVerbs should let you know.

Score: 10

Try using:

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

0

Score: 3

not entirely sure if it is the correct way, but 3 i would use a meaningless parameter to differentiate 2 the sigs. like:

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends(bool isGet)
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

I know it's ugly and hackish, but 1 it works.

Score: 3

Marking cagdas' response as the answer since 5 it answered my question. However, since 4 I don't like using the ActionName attribute 3 in my project I use a different solution. I 2 simply added the FormCollection to the "post" action 1 (which ends up changing the method signature)

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends(FormCollection form)
{
    // do some stuff
    return View();
}
Score: 1

add to the Post method the params to want 4 to receive in the post. maybe like this:

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends(string friendName, string otherField)
{
    // do some stuff
    return View();
}

..or 3 if you have a complex type, like this:

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends(Friend friend)
{
    // do some stuff
    return View();
}

Edit: It 2 would be preferable to use a more typed-approach 1 to receiving the posted items, like above.

Score: 0

Your action methods can't be doing the same 10 thing, otherwise there would be no need 9 to to write two action methods. So if the 8 semantics are different, why not use different 7 names for the action methods as well?

For 6 example, if you had a "delete" action method 5 and GET just asks for confirmation, you 4 might call the GET method "ConfirmDelete" and 3 the POST method just "Delete".

Not sure 2 if that matches your scenario, but it always 1 did for me when I had the same problem.

More Related questions