【问题标题】:Asp.Net MVC - Redirect to external URL and Post the data from ActionAsp.Net MVC - 重定向到外部 URL 并从 Action 发布数据
【发布时间】:2019-09-26 04:53:42
【问题描述】:

我有一个要求,我需要将用户从 MVC Action 方法重定向到另一个外部 URL,并且还需要将某些数据发布到外部 URL。请提出可能的选项。

谢谢

【问题讨论】:

  • 在视图中使用 ajax 发布外部请求,然后重定向到内部操作
  • 你能分享任何参考链接吗

标签: asp.net-mvc


【解决方案1】:

你可以试试这样的

$(function(){

// button handle the action where you want to post data and redirect
  $("#btnSubmit").click(function(){

 //ajax post to internal action
   $.ajax({
      type: "POST",
      url: '@Url.Action("ActionName","Controller"),
      data: json_object_to_post,
      success: function (result) {
           // handle the result here if required
    }
   });

   //redirect to the external action
    window.location.href = "external_url";
  });

【讨论】:

  • 正如我所提到的,我想从 Action 方法重定向到外部 URL 并同时发布数据。
  • 同理,ajax会post到你的action,window.location指向你的外部url
  • 我不想发布到我的操作中......从我的操作中我想将数据发布到外部 URL 并将用户重定向到该 URL
  • 在这种情况下,您应该使用 HttpClient 从控制器向外部站点发送 HTTP 请求
  • 重定向怎么样?
【解决方案2】:

考虑有两个动作:

 public ActionResult First()
    {
    //here store the data wither in session or tempdata
    //session["data"]=postdata;
    //temp["data"]=postdata
       return RedirectToAction(“Second”,”ControllerName”);
    }

public ActionResult Second()
{
//check for the session or temp variables
return View();
}

通过这种方式,您可以将数据从一个网址传递到另一个网址

【讨论】:

    猜你喜欢
    • 2010-09-05
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2013-09-19
    • 2013-03-11
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多