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

时间秒杀一切 提交于 2021-02-07 19:21:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!