Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: RedirectToAction with parameters to POST Action

This question has been asked here:

RedirectToAction with parameter

But what if I have two actions with the same name but different parameters? How do I redirect to the POST Terms action instead of the GET Terms action.

public ActionResult Terms() {
    //get method
}

[HttpPost]
public ActionResult Terms(string month, string year, int deposit = 0, int total = 0) {
    //process POST request
}
like image 1000
Rosdi Kasim Avatar asked Sep 06 '25 03:09

Rosdi Kasim


2 Answers

Nevermind guys, actually I could just call the method directly instead of using RedirectToAction like so:

return Terms(month, year, deposit, total);

Instead of:

return RedirectToAction("Terms", {month, year, deposit, total});
like image 77
Rosdi Kasim Avatar answered Sep 07 '25 23:09

Rosdi Kasim


You are correct that you can call the method directly, but I would highly suggest that you rethink your architecture/implementation.

The HTTP Protocol embraces the idea of safe and unsafe verbs. Safe verbs like GET are not suppose to modify the state of the server in any way, whilst Unsafe verbs like POST, PUT do modify state. By you GET calling the POST method you are violating this principle since it is not inconceivable that your POST is going to be modifying state.

Also best practice dictates that you should limits the verbs on all your actions so if the first 'Terms' method is meant as a GET, then also add the HttpGet attribute to it to prevent other Http actions from being accepted by the server for the action.

like image 36
Sarel Esterhuizen Avatar answered Sep 07 '25 22:09

Sarel Esterhuizen