问题
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.
回答1:
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.
回答2:
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"];
来源:https://stackoverflow.com/questions/18045600/how-to-redirect-to-an-external-url-with-post-parameters-in-a-controller