GET and POST methods with the same Action name in the same Controller
Since you cannot have two methods with the same name and signature you have to use the ActionName
attribute:
[HttpGet]
public ActionResult Index()
{
// your code
return View();
}
[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost()
{
// your code
return View();
}
Also see "How a Method Becomes An Action"
Same method in controller accept GET and POST
You could decorate the action with both [HttpGet]
and [HttpPost]
attributes and use [FromQuery]
to bind the parameters (assuming POST will send the parameters in the URL too).
[HttpGet]
[HttpPost]
public async Task<string> SendMessage([FromQuery]string destination, [FromQuery]string message)
{
...
}
I agree with the other answer in that SendMessage
should really be a POST. However, if you need to access this action method using both GET and POST then perhaps this solution will work for you.
MVC - GET & POST actions with the same name and parameters in the same controller
Do you really need a model
param at GET Create
action? You can do something like this:
public ActionResult Create()
{
var model = new CreateBananaViewModel();
return View(model);
}
or, if you wish to receive some query data to the action (www.mysite.com/banana/create?bananaType=yellow
)
public ActionResult Create(string bananaType, string anotherQueryParam)
{
var model = new CreateBananaViewModel()
{
Type = bananaType
};
return View(model);
}
and leave your POST action as it is
[HttpPost]
public ActionResult Create(CreateBananaViewModel model) {}
Use Get And Post in the same Controller
If you really want to do that, you can use the [AcceptVerbs]
attribute. (See this SO question)
This way your method can handle the GET and POST verbs (but not others like PUT)
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
//Code...
return View();
}
If you want your method to handle all verbs, don´t use any attribute at all:
public ActionResult Index(FormCollection form)
{
//Code...
return View();
}
@Url.Action("Action","Controller") calls both post and get method with same name
Issue was I was having one more ajax request to check if the page has been loaded completely to show progress bar, when removed that portion of the code from layout, It has been working fine without calling any action method twice.
AspCore Multiple Post Actions with Same Action Name
You cannot. There is no way for Route Engine to differentiate those 3 post methods.
You could append something at the end to the URL to make them different.
[HttpPost]
[Route("checkout/{guid}/paypal")]
public IActionResult Checkout([FromRoute] String guid, Method1 model)
{
....
}
[HttpPost]
[Route("checkout/{guid}/authorizenet")]
public IActionResult Checkout([FromRoute] String guid, Method2 model)
{
....
}
Testing GET/POST methods in controller with same name in ASP.MVC
You can also use the parameterize the calls.
[HttpGet]
public ActionResult Edit(int? id)
{
return View();
}
[HttpPost]
public ActionResult Edit(Object myObject)
{
return View();
}
.
var controller = new Controller();
controller.Edit(myObject: null);
controller.Edit(id: null);
Related Topics
Client Specific Role Based Authentication
How to Secure the Asp.Net_Sessionid Cookie
Linq: Select an Object and Change Some Properties Without Creating a New Object
Copy Few Items from One List to Another List of Same Type in C#
Passing a List of Objects into an MVC Controller Method Using Jquery Ajax
How to Check for a Valid Base64 Encoded String
How to Get User Browser Name ( User-Agent ) in ASP.NET Core
How to Wait Until Task Is Finished in C#
How to Fix Json.Net (Newtonsoft.Json) Runtime File Load Exception
Pass Variable in Json String in C#
Trying to Add Spaces Between Characters in a String in C#
C# Windows Form Application for Employee Management
How to Print the Elements With Text Value That Contains in a List Selenium C#
Ssh.Net Sftp Get a List of Directories and Files Recursively
Asp Core Webapi Test File Upload Using Postman
Update Value in Datatable from Another Datatable
Using Newtonsoft to Deserialize a Date Stamp That Might Consist Only of a Year