Chris Pietschmann
Chris Pietschmann

Reputation: 29905

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

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?

Upvotes: 157

Views: 191273

Answers (8)

estinamir
estinamir

Reputation: 503

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
        }
     }

Upvotes: 0

Carlos Alberto
Carlos Alberto

Reputation: 1

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

Upvotes: 0

Jason Bunting
Jason Bunting

Reputation: 58961

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.

Upvotes: 174

PowerMan2015
PowerMan2015

Reputation: 1418

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

Upvotes: 1

Yitzhak Weinberg
Yitzhak Weinberg

Reputation: 2592

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

Upvotes: 5

vicky
vicky

Reputation: 1580

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

Upvotes: 14

Otto Kanellis
Otto Kanellis

Reputation: 3708

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.

Upvotes: 26

Eli Courtwright
Eli Courtwright

Reputation: 193241

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.

Upvotes: 125

Related Questions