Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to post action from another action

I want to redirect to a post action from another action in an MVC3 Controller. How can I do this?

like image 244
Mohammad Akbari Avatar asked Nov 12 '22 14:11

Mohammad Akbari


1 Answers

There are two basic ways to do that:

  • create a <form> that has the action set to a URL that points to the action you want to call.

  • do a POST ajax request from the client

Update

To redirect, you can simply return a RedirectToRouteResult. Usually that's done using one of the RedirectToAction overloads available on Controller.

Update 2

If the target action is POST only (let's assume it's called TargetAction), you could create a new action that allows GET and does return TargetAction().

A pure redirect is not possible, because AFAIK redirect means that:

  • the server returns a 3xx redirect status together with the new location of the resource
  • the browser does a GET request to the location indicated at the previous step.

However, I suggest that you rethink the design of your controller actions to avoid this situation if possible.

like image 75
Cristian Lupascu Avatar answered Dec 20 '22 04:12

Cristian Lupascu