Andrzej Gis
Andrzej Gis

Reputation: 14316

How to redirect to an external URL with POST parameters in a controller

How can I redirect from a controller to an external URL using POST method and passing some parameters?

Basically I need to do automatically what a form would do.

I found Redirect method in my controller, but it only seems to accept a url string. No method nor parameters.

Upvotes: 4

Views: 8469

Answers (2)

Marcus Rabello
Marcus Rabello

Reputation: 106

I'm using Fluentx.Mvc from Nuget for this.

Install Fluentx.Mvc from nuget

You need to include in your code:

using Fluentx.Mvc;

and the code to call a external URL with post:

First create a Dictionary like:

Dictionary<string, object> 
          objData = new Dictionary<string, object>();

and insert values:

objData.Add("name", "John");

objData.Add("city", "NY");

After that, use return from Fluentx:

return this.RedirectAndPost("http://yourexternalurl", objData);

In your external url, you get values :

string strName = Request["nome"];
string strCity = Request["city"];

Upvotes: 4

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

You can't do post with server side redirect. Options:

  • perform POST on server and handle results servers side (does not work if you need cookies to be set or used on destination server by that post request)
  • perform post to that server directly on browser side
  • perform AJAX post to your server and normal post to destination server if you need to notify both.

Upvotes: 2

Related Questions