4

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.

Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
  • Do you need to redirect to the form after posting to it ? – Ofiris Aug 04 '13 at 17:41
  • Possible duplicate of [Response.Redirect with POST instead of Get?](http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get) – Uriil Feb 06 '16 at 16:06

2 Answers2

4

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"];
Kuldeep Dubey
  • 1,097
  • 2
  • 11
  • 33
2

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.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179