Rosdi Kasim
Rosdi Kasim

Reputation: 25956

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
}

Upvotes: 29

Views: 88047

Answers (2)

Sarel Esterhuizen
Sarel Esterhuizen

Reputation: 1688

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.

Upvotes: 8

Rosdi Kasim
Rosdi Kasim

Reputation: 25956

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});

Upvotes: 64

Related Questions