How do you redirect to a page using the POST verb?

asp.net MvcUrl Redirection

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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.

Hope that helps.

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

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.

Solution 4 - asp.net Mvc

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>");

Solution 5 - asp.net Mvc

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

Solution 6 - asp.net Mvc

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");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris PietschmannView Question on Stackoverflow
Solution 1 - asp.net MvcJason BuntingView Answer on Stackoverflow
Solution 2 - asp.net MvcEli CourtwrightView Answer on Stackoverflow
Solution 3 - asp.net MvcOtto KanellisView Answer on Stackoverflow
Solution 4 - asp.net MvcvickyView Answer on Stackoverflow
Solution 5 - asp.net MvcYitzhak WeinbergView Answer on Stackoverflow
Solution 6 - asp.net MvcPowerMan2015View Answer on Stackoverflow