Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

post and get with same method signature

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?

like image 684
codette Avatar asked Apr 07 '09 06:04

codette


2 Answers

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(); } 
like image 158
Çağdaş Tekin Avatar answered Oct 12 '22 01:10

Çağdaş Tekin


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.

like image 39
mohrtan Avatar answered Oct 12 '22 00:10

mohrtan