简体   繁体   English

从管制员行动发布到第三方网站

[英]Post from controller action to third party website

I have an MVC page with a form which when submitted posts firstly to my controller and handled by my controller action. 我有一个带有窗体的MVC页面,该窗体首先提交到我的控制器并由我的控制器动作处理。 At the controller action I manipulate the posted data and then need to re-POST that data to a third party webpage. 在控制器操作中,我操纵发布的数据,然后需要将该数据重新发布到第三方网页。

How do I redirect and re-POST form values to a third party web page from within an MVC post action? 如何在MVC发布操作中将表单值重定向并重新发布到第三方网页?

Update 更新

This is possible in .Net MVC and WebForms. 在.Net MVC和WebForms中,这是可能的。 See solution below. 请参阅下面的解决方案。

You need to handle the [Post] Action by: 您需要通过以下方式处理[发布]操作:

  1. Adding a location header specifying the page to redirect to. 添加一个位置标头,指定要重定向到的页面。
  2. Return a temporary redirect using an instance HttpStatusCodeResult. 使用实例HttpStatusCodeResult返回一个临时重定向。

You must use the 307 Temporary Redirect status response code as it maintains the POST method of the request. 您必须使用307临时重定向状态响应代码,因为它维护了请求的POST方法。 If you use the 302 status response, the browser will convert the request into a GET request and drop the form values that were originally posted. 如果使用302状态响应,浏览器会将请求转换为GET请求,并删除最初发布的表单值。

How does it work? 它是如何工作的?

This works by having your server communicate back to the browser that the location of the page has changed. 这可以通过使服务器与浏览器通信来通知页面位置已更改,从而起作用。 The Browser then rePOST's the form data to the new page location. 然后,浏览器将表单数据重新发布到新页面位置。

Example of usage below: 以下用法示例:

    [HttpPost]
    public ActionResult Test()
    {
        var amount = Request["amount"];

        string url = "/Home/Test2";
        HttpContext.Response.AddHeader("Location", url);
        return new HttpStatusCodeResult(307);
    }

    public ActionResult Test2()
    {
        var x = Request["amount"];
        Response.Write("Stuff 123...." + x);
        return View();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控制器动作在Ajax帖子中被调用了两次 - Controller action called twice from Ajax post 来自MVC中其他控制器的呼叫后操作 - Call Post Action From Different Controller In MVC 将整数id传递给控制器​​中从角度4开始的操作 - Passing an integer id to post action in controller from angular 4 .NET MVC Controller操作未由Flex应用程序进行GET或POST调用 - .NET MVC Controller action not invoked by GET or POST from Flex app CodeIgniter third_party - CodeIgniter third_party 当第三方客户致电我的网络服务时致电控制器 - Call Controller when third party client call my web-service MVC控制器操作对象参数从AJAX POST接收空值 - MVC Controller Action Object Parameter Receives Null Values from AJAX POST Laravel从其他控制器调用控制器动作 - Laravel call controller action from other controller 从视图中调用控制器动作…还是不? - Calling a Controller Action from a View… Or not? When I try to post 200 plus records through model list from view to controller it is showing always null in controller HTTP POST Action in MVC - When I try to post 200 plus records through model list from view to controller it is showing always null in controller HTTP POST Action in MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM