GET and POST with the same Action name in the same controller

Why is this wrong?

{ public class HomeController : Controller { [HttpGet] public ActionResult Index() { Some Code--Some Code---Some Code return View(); } [HttpPost] public ActionResult Index() { Some Code--Some Code---Some Code return View(); } } 

How can I get a control packet that responds to one when it is "getted" and one is "sent"?

+76
c # asp.net-mvc asp.net-mvc-3
Mar 04 2018-12-12T00:
source share
7 answers

Since you cannot have two methods with the same name and signatures, you should use the ActionName attribute:

  [HttpGet] public ActionResult Index() { Some Code--Some Code---Some Code return View(); } [HttpPost] [ActionName("Index")] public ActionResult IndexPost() { Some Code--Some Code---Some Code return View(); } 

Also see "How a Method Becomes an Action"

+162
Mar 04 '12 at 6:13
source share

Although ASP.NET MVC will allow you to have two actions with the same name, .NET will not allow you to have two methods with the same signature - that is, with the same name and parameters.

You will need to specify the methods differently using the ActionName attribute to tell ASP.NET MVC that they are actually the same action.

However, if you are talking about GET and POST, this problem is likely to disappear, since the POST action will take more parameters than GET, and therefore can be distinguishable.

So you need to:

 [HttpGet] public ActionResult ActionName() {...} [HttpPost, ActionName("ActionName")] public ActionResult ActionNamePost() {...} 

Or

 [HttpGet] public ActionResult ActionName() {...} [HttpPost] public ActionResult ActionName(string aParameter) {...} 
+36
Mar 04 '12 at 6:26
source share

I like to receive a form post for my POST actions, even if I don't need it. For me, it just seems to work right, as you are supposedly posting something .

 public class HomeController : Controller { public ActionResult Index() { //Code... return View(); } [HttpPost] public ActionResult Index(FormCollection form) { //Code... return View(); } } 
+16
Aug 09 2018-12-12T00:
source share

To answer your specific question, you cannot have two methods with the same name and the same arguments in the same class; using the HttpGet and HttpPost attributes does not distinguish between methods.

To solve this problem, I usually include a presentation model for the form you post:

 public class HomeController : Controller { [HttpGet] public ActionResult Index() { Some Code--Some Code---Some Code return View(); } [HttpPost] public ActionResult Index(formViewModel model) { do work on model -- return View(); } } 
+5
Mar 04 '12 at 6:20
source share

Impossible multi-user same name and same parameter

  [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(int id) { return View(); } 

although int int id is not used

+2
Mar 04 '12 at 6:15
source share

You cannot have multiple actions with the same name. You can add a parameter to one method and this will be valid. For example:

  public ActionResult Index(int i) { Some Code--Some Code---Some Code return View(); } 

There are several ways to do actions that differ only in the request verb. My favorite and, I think, the easiest way to use the AttributeRouting package. After installation, simply add the attribute to your method as follows:

  [GET("Resources")] public ActionResult Index() { return View(); } [POST("Resources")] public ActionResult Create() { return RedirectToAction("Index"); } 

In the above example, the methods have different names, but the action name in both cases is β€œResources”. The only difference is the request verb.

The package can be installed using NuGet as follows:

PM> Install-Package AttributeRouting

If you do not need dependency on AttributeRouting packages, you can do this by writing a special attribute for the action selector.

+2
Mar 04 '12 at 7:05
source share

You got a good answer to this question, but I want to add my two cents. You can use one method and process requests according to the type of request:

 public ActionResult Index() { if("GET"==this.HttpContext.Request.RequestType) { Some Code--Some Code---Some Code for GET } else if("POST"==this.HttpContext.Request.RequestType) { Some Code--Some Code---Some Code for POST } else { //exception } return View(); } 
+1
Dec 17 '15 at 10:07
source share



All Articles