【问题标题】:ASP.NET MVC 3 - Replacing HttpContext Response Not WorkingASP.NET MVC 3 - 替换 HttpContext 响应不起作用
【发布时间】:2012-02-06 14:34:24
【问题描述】:

我正在使用最近升级为使用 MVC3 的 Nopcommerce - 以前它使用网络表单。我正在尝试连接到 Worldpay 的(支付网关)托管站点。这个过程有点复杂,但基本上需要向 Worldpay 提交一个表单,然后用户会被重定向到他们的托管网站以填写他们的信用卡详细信息等。

Nopcommerce 使用 Post 方法处理此问题,该方法在服务器端构建需要发布的表单并用构建的 HTML 替换 httpcontext 响应

_httpContext.Response.Clear();
_httpContext.Response.Write("<html><head>");
_httpContext.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
_httpContext.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
for (int i = 0; i < _inputValues.Keys.Count; i++)
    _httpContext.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", HttpUtility.HtmlEncode(_inputValues.Keys[i]), HttpUtility.HtmlEncode(_inputValues[_inputValues.Keys[i]])));
_httpContext.Response.Write("</form>");
_httpContext.Response.Write("</body></html>");
_httpContext.Response.End();

这在 webforms 版本中工作正常,但在 MVC 版本中不起作用。没有错误,一切似乎都正常工作。但是,处理继续,执行从服务层(该方法所在的层)返回到控制器,并发出以下重定向:

 return RedirectToRoute("CheckoutCompleted");

在修改服务器端的响应方面,MVC 与 Web 表单的工作方式是否不同?我已经尝试了以下代码,并且可以确认 worldpay 确实被击中并且正在发送我期望的响应:

_httpContext.Response.Clear();
var webClient = new WebClient();
var responseBytes = webClient.UploadValues(Url, "POST", _inputValues);
var response = Encoding.UTF8.GetString(responseBytes);
_httpContext.Response.Write(response);
_httpContext.Response.End();

注意:在上述示例中,HttpContext 被注入到服务中。通过使用 HttpContext.Current

可以实现相同的结果

更新:

再做一点挖掘,我发现如果我从任何控制器写入响应流并结束响应,我会得到一个空白(0 长度响应)屏幕。以下是导致上述问题的家庭控制器上的索引操作。同样的代码,在我创建的一次性项目中,完美运行......

  public ActionResult Index()
  {    
            Response.Write("test");
            Response.End();
            return View(_storeInformationSettings);
  }

【问题讨论】:

  • 对两者都使用 fiddler 并比较响应。关于“mvc 的工作方式是否不同”的问题有点加载,因为它取决于 Web 表单管道与 mvc 控制器中的任何操作。一般来说,“否”,但这取决于返回的内容类型等。
  • 我测试过,对我来说效果很好。我的意思是,我可以通过response.Write() 在屏幕上书写,并且在response.End() 之后没有执行我对return RedirectToRoute("CheckoutCompleted"); 的调用
  • 我有同样的问题,但还没有看到解决方案。如果我把 Response.Write("somestring");响应。结束();在控制器中我没有问题。但是,如果我在课堂上使用它,无论我在课堂上使用什么,都会出错。我尝试过:(1)将 HttpContext 从控制器传递给方法(2)将 ControllerContext 传递给方法,然后使用 HttpContext 和(3)HttpContext.Current。所有这些都不起作用。
  • 更新:找到了一个解决方案,我会把它放在答案中。

标签: asp.net-mvc-3 nopcommerce worldpay


【解决方案1】:

如果您更改代码的第三个 sn-p,则可以将 Response.Write 和 Response.End 留在控制器之外。

HttpContext.Current.Response.Clear();
var webClient = new WebClient();
var responseBytes = webClient.UploadValues(Url, "POST", _inputValues);
var response = Encoding.UTF8.GetString(responseBytes);
HttpContext.Current.Response.Write(response);
HttpContext.Current.ApplicationInstance.CompleteRequest();

HttpContext.Current.ApplicationInstance.CompleteRequest(); 为我解决了问题。另请参阅有关此MSDN Blog about Response.End and Response.Close

的好帖子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 2012-03-31
    • 2014-09-28
    • 2013-04-27
    相关资源
    最近更新 更多