Mohammad Akbari
Mohammad Akbari

Reputation: 4766

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?

Upvotes: 5

Views: 6835

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Why? You cannot redirect to a post action by http verb definition.

Why not just call your other action method directly rather than using a redirect?

Upvotes: 0

Cristian Lupascu
Cristian Lupascu

Reputation: 40516

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.

Upvotes: 1

Related Questions