157

When you call RedirectToAction within a controller, it automatically redirects using an HTTP GET. How do I explicitly tell it to use an HTTP POST?

I have an action that accepts both GET and POST requests, and I want to be able to RedirectToAction using POST and send it some values.

Like this:

this.RedirectToAction( "actionname", new RouteValueDictionary(new { someValue = 2, anotherValue = "text" }) ); 

I want the someValue and anotherValue values to be sent using an HTTP POST instead of a GET. Does anyone know how to do this?

1
  • 1
    The answer posting by jason will work in most scenarios, the only problem which i see is that it is accident prone. i.e. Calling a action method directly bypasses all filters applied to the action. So , in case there is any authentication or counter sort of filter applied to the action method, that data might be lost. Calling a action method directly will work, but it should be carefully applied. Commented Feb 26, 2012 at 17:36

8 Answers 8

173

For your particular example, I would just do this, since you obviously don't care about actually having the browser get the redirect anyway (by virtue of accepting the answer you have already accepted):

[AcceptVerbs(HttpVerbs.Get)] public ActionResult Index() { // obviously these values might come from somewhere non-trivial return Index(2, "text"); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(int someValue, string anotherValue) { // would probably do something non-trivial here with the param values return View(); } 

That works easily and there is no funny business really going on - this allows you to maintain the fact that the second one really only accepts HTTP POST requests (except in this instance, which is under your control anyway) and you don't have to use TempData either, which is what the link you posted in your answer is suggesting.

I would love to know what is "wrong" with this, if there is anything. Obviously, if you want to really have sent to the browser a redirect, this isn't going to work, but then you should ask why you would be trying to convert that regardless, since it seems odd to me.

Sign up to request clarification or add additional context in comments.

10 Comments

I'm not a downvoter, but the one caution with this is if you were to call a view with a different name, or if the parameters are important, they are lost. The reason being is the URL will reflect the action+parameters before the server side redirect. This can lead to confusion by the user, especially if they refreshed the page and then found themselves at a previous page(because the refresh used the old URL). This technique is essentially very similar to asp.net's Server.Transfer, and the same cautions should be exercised.
I didn't downvote per se but I can see reason to. This method violates the coding convention set up by the MVC pattern. It only works when calling the same action. If the action is another, even on the same controller, the routing values are screwed and the wrong view will be returned. In short: Don't do this.
@erlando What do you mean it only works when the action is the same action? I do this all the time to call other actions. The only issue I've encountered is that when you return View(), you need to specify the appropriate view, or it will think you want the view from the calling action.
I might be stupid, but this does not even answers the question, if i'm in Index2() then still, I can't directly call the POST action of Index1(string s, int i)
In some circumstances this will work, but not ideal. I'm actually on SO because actions identical to this have caused problems in our source-code & I'm trying to fix it. As @erlando noted, routing values are screwed. You'll also get messed up ModelState (If you call ModelState.IsValid within the "Post" action, it will validate the "Get" action's model instead), action filters may not be applied, etc. @vicky's answer seems to be the only one that's generally appropriate here & directly answers the question, but it's a bit of a hack.
|
125

HTTP doesn't support redirection to a page using POST. When you redirect somewhere, the HTTP "Location" header tells the browser where to go, and the browser makes a GET request for that page. You'll probably have to just write the code for your page to accept GET requests as well as POST requests.

2 Comments

Curious why my answer isn't accepted, I think my rhetoric is sound. :) Then again, I may be a bit biased about it...
While this answer is basically correct, it is not complete. See Jason Bunting answer below for a much better workaround.
26

If you want to pass data between two actions during a redirect without include any data in the query string, put the model in the TempData object.

ACTION

TempData["datacontainer"] = modelData;

VIEW

var modelData= TempData["datacontainer"] as ModelDataType; 

TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this.

Therefore, the only scenario where using TempData will reliably work is when you are redirecting.

Comments

14

try this one

return Content("<form action='actionname' id='frmTest' method='post'><input type='hidden' name='someValue' value='" + someValue + "' /><input type='hidden' name='anotherValue' value='" + anotherValue + "' /></form><script>document.getElementById('frmTest').submit();</script>"); 

2 Comments

Hate it but love it :)
Such a hack but it was the only way I could do what I wanted without violating DRY or rewiring my whole setup! Thanks!
5

I would like to expand the answer of Jason Bunting

like this

ActionResult action = new SampelController().Index(2, "text"); return action; 

And Eli will be here for something idea on how to make it generic variable

Can get all types of controller

1 Comment

You shouldn't create an instance to a controller with new ...() because you will lose the RequestContext - if you are already in the same controller, you might not need to create a new instance. Otherwise, take the following way: SampelController sampleController = DependencyResolver.Current.GetService<SampelController>() then: sampleController.ControllerContext = new ControllerContext(Request.RequestContext, sampleController); then you can return sampleController.Index(2, "text"); Just a hint :)
1

I have just experienced the same problem.

The solution was to call the controller action like a function:

return await ResendConfirmationEmail(new ResendConfirmationEmailViewModel() { Email = input.Email }); 

The controller action:

[HttpPost] [AllowAnonymous] public async Task<IActionResult> ResendConfirmationEmail(ResendConfirmationEmailViewModel input) { ... return View("ResendConfirmationEmailConfirmed"); } 

1 Comment

This is good but will only be possible within the same controller. From another controller, you'd need an instance to call the method, like possibly new EmailController().ResendConfirmationEmail(...); ?
0

Try this: In your view from Page1

<form method="post"> <input name="text" /> <button type="submit">Send</button> </form> 

In your control from page1

public IActionResult OnPost(string text){ return RedirectToPagePreserveMethod("Page2"); } 

In your control from Page2

public IActionResult OnPost(string text){ ViewData["text"] = text; return Page(); } 

In your View from Page2

<h1>@ViewData["Text"]</h1> 

I Tried in .NetCore 6

Comments

0

Here we're changing PowerMan's answer to do with direct function call within the same controller.

Controller 1:

return RedirectToAction("Index2", "CustomerGuestLogin", new CustomerGuestModel() { Email = model.Email, Password = model.Password }); 

Controller 2:

 [HttpGet] public ActionResult Index2(CustomerGuestModel e) { return this.Index(e); } [HttpPost] public ActionResult Index(CustomerGuestModel e) { string status = ""; if (!string.IsNullOrEmpty(e.Email) && !string.IsNullOrEmpty(e.Password)) { //do something } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.