post and get with same method signature

asp.net MvcPostControllerGetAsp Net-Mvc-1

asp.net Mvc Problem Overview


In my controller I have two actions called "Friends". The one that executes depends on whether or not it's a "get" versus a "post".

So my code snippets look something like this:

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

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

However, this does not compile since I have two methods with the same signature (Friends). How do I go about creating this? Do I need to create just one action but differentiate between a "get" and "post" inside of it? If so, how do I do that?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Rename the second method to something else like "Friends_Post" and then you can add [ActionName("Friends")] attribute to the second one. So the requests to the Friend action with POST as request 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();
}

Solution 2 - asp.net Mvc

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

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

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

Solution 3 - asp.net Mvc

Try using:

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

Solution 4 - asp.net Mvc

not entirely sure if it is the correct way, but i would use a meaningless parameter to differentiate 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 it works.

Solution 5 - asp.net Mvc

Marking cagdas' response as the answer since it answered my question. However, since I don't like using the ActionName attribute in my project I use a different solution. I simply added the FormCollection to the "post" action (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();
}

Solution 6 - asp.net Mvc

add to the Post method the params to want 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 if you have a complex type, like this:

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

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

Solution 7 - asp.net Mvc

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

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

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

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
QuestioncodetteView Question on Stackoverflow
Solution 1 - asp.net MvcÇağdaş TekinView Answer on Stackoverflow
Solution 2 - asp.net MvcmohrtanView Answer on Stackoverflow
Solution 3 - asp.net MvcKateAView Answer on Stackoverflow
Solution 4 - asp.net MvcArielView Answer on Stackoverflow
Solution 5 - asp.net MvccodetteView Answer on Stackoverflow
Solution 6 - asp.net MvcMatt KocajView Answer on Stackoverflow
Solution 7 - asp.net MvcAdrian GrigoreView Answer on Stackoverflow