28

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
}
Community
  • 1
  • 1
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154

2 Answers2

63

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});
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
  • 8
    It is the solution. But it seems slightly like a hack: address in the browser is still old method. That way you can't see this, because both methods are the same name. But the problem appears when your method with arguments would be called i.e. "TermsPost". What to do to get "TermsPost" (not "Terms") in the browser address bar? – pt12lol Sep 25 '13 at 13:30
  • Agree with pt12lol, It retains the URL with originally called action. – Tejasvi Hegde Jul 29 '15 at 12:00
  • @pt12lol : There are two issues here, the browser really doesn't need to know what MVC action the page came from, RedirectToAction should be for internal programming management not the browser. – jimjim Oct 11 '16 at 01:08
  • 1
    This method will not work if views associated to these actions have different viewModels because the returned action will return the view back to the first action therefore you will get a `System.InvalidOperationException` with message : The model item passed into the dictionary is of type 'type1', but this dictionary requires a model item of type 'type2'. – AmiNadimi Nov 26 '16 at 15:26
  • Isn't the correct approach to extract the functionality you wish to implement from Terms. Then call your shared logic from any applicable controller actions? – JDNickell Feb 13 '17 at 19:10
  • This option will not work if you have any dependencies on accurate request data in your POST methods. Objects like {User} and {Request} will be null as the controller has not been called by a web request. – Richard Feb 04 '19 at 13:43
8

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.

Sarel Esterhuizen
  • 1,628
  • 17
  • 18