153

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
  • 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. Feb 26, 2012 at 17:36

7 Answers 7

169

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.

13
  • 7
    Who knows why you were downvoted. This is a very useful method.
    – Peter J
    Jun 3, 2010 at 18:25
  • 2
    This is how I always solved this problem as well. Downvoting this makes no sense. Jan 10, 2011 at 13:08
  • 41
    I voted up, although I disagree with calling people idiots when you don't know them. Jan 18, 2011 at 21:38
  • 3
    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.
    – AaronLS
    Jun 26, 2012 at 15:40
  • 17
    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
    Sep 24, 2012 at 14:01
122

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
  • 4
    Curious why my answer isn't accepted, I think my rhetoric is sound. :) Then again, I may be a bit biased about it... Nov 24, 2010 at 22:23
  • 16
    While this answer is basically correct, it is not complete. See Jason Bunting answer below for a much better workaround. Jan 10, 2011 at 13:07
24

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.

13

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
  • 6
    Hate it but love it :)
    – divinci
    Jan 24, 2019 at 13:29
  • Such a hack but it was the only way I could do what I wanted without violating DRY or rewiring my whole setup! Thanks!
    – jamheadart
    May 7, 2020 at 8:49
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
  • 7
    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 :) Jul 31, 2020 at 6:35
0

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
  • 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(...); ?
    – SNag
    Mar 9, 2022 at 20:06
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.