ASP.NET MVC 3 - 更换HttpContext的响应不工作 [英] ASP.NET MVC 3 - Replacing HttpContext Response Not Working

查看:169
本文介绍了ASP.NET MVC 3 - 更换HttpContext的响应不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Nopcommerce它最近已经升级到使用MVC3

- previously它使用web表单。我试图连接到WorldPay工作的(支付网关)主办的网站。这个过程是一个有点令人费解,但本质上是一个表单需要提交给WorldPay工作,然后将用户重定向到其托管的网站,以填补他们的信用卡详细信息等。

Nopcommerce借此使用POST方法当中,在服务器端,构建一个需要被公布并取代内置了HTML

HttpContext的反应形式的护理

  _httpContext.Response.Clear();
_httpContext.Response.Write(< HTML和GT;< HEAD>中);
_httpContext.Response.Write(的String.Format(< /头><身体的onload = \\文件{0} .submit()\\>中,窗体名称));
_httpContext.Response.Write(的String.Format(<表格名称= \\{0} \\的方法= \\{1} \\行动= \\{2} \\>中,窗体名称,方法,URL ));
的for(int i = 0; I< _inputValues​​.Keys.Count;我++)
    _httpContext.Response.Write(的String.Format(<输入名字= \\{0} \\式= \\隐藏\\价值= \\{1} \\>中,HttpUtility.HtmlEn code (_inputValues​​.Keys [I]),HttpUtility.HtmlEn code(_inputValues​​ [_inputValues​​.Keys [I])));
_httpContext.Response.Write(< /形式为GT;);
_httpContext.Response.Write(< /身体GT;< / HTML>中);
_httpContext.Response.End();

这工作在web表单版本罚款,但在MVC版本不起作用。没有错误,并且一切似乎都正常工作。然而,处理继续并且执行从服务层返回(在此方法所在地)给控制器,并在下重定向发:

 返回RedirectToRoute(CheckoutCompleted);

是否MVC不同工作的WebForms关于修改服务器端的响应?我曾尝试以下code和可以确认,WorldPay的确实是被击中并正在发送我期待的回应:

  _httpContext.Response.Clear();
VAR的WebClient =新的WebClient();
VAR responseBytes = webClient.UploadValues​​(URL,POST,_inputValues​​);
VAR响应= Encoding.UTF8.GetString(responseBytes);
_httpContext.Response.Write(响应);
_httpContext.Response.End();

注意:在上述实施例中所述的HttpContext注入服务。相同的结果是通过使用HttpContext.Current

实现

更新:

做多一点挖我看到,如果我从任何控制器写入响应流,并最终响应得到了一个空白(0长度响应)屏幕。下面是家用控制器,使所述问题上的索引操作。同样的code,在我创建了一个一次性项目,工程完美...

 公众的ActionResult指数()
  {
            的Response.Write(测试);
            到Response.End();
            返回查看(_storeInformationSettings);
  }


解决方案

如果你改变了$ C $的第三个片段c可以离开的Response.Write并到Response.End你的控制器。

  HttpContext.Current.Response.Clear();
VAR的WebClient =新的WebClient();
VAR responseBytes = webClient.UploadValues​​(URL,POST,_inputValues​​);
VAR响应= Encoding.UTF8.GetString(responseBytes);
HttpContext.Current.Response.Write(响应);
HttpContext.Current.ApplicationInstance.CompleteRequest();

HttpContext.Current.ApplicationInstance.CompleteRequest(); 的伎俩我。又见好贴这个<一个href=\"http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx\"相对=nofollow> MSDN博客约到Response.End和Response.Close

I am using Nopcommerce which has recently been upgraded to use MVC3 - previously it used webforms. I am attempting to connect to Worldpay's (payment gateway) hosted site. The process is a little convoluted but essentially a form needs to be submitted to Worldpay and the user is then redirected to their hosted site to fill in their credit card details etc.

Nopcommerce takes care of this using a Post method which, on the server side, builds the form that needs to be posted and replaces the httpcontext response with built out HTML

_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();

This works fine in the webforms version but does not work in the MVC version. There is no error, and everything appears to work correctly. However, processing continues and execution returns from the service layer (in which this method is located) to the controller and the following redirect is issued:

 return RedirectToRoute("CheckoutCompleted");

Does MVC work differently to webforms with regard to modifying the response on the server side? I have tried the following code and can confirm that worldpay is indeed being hit and is sending the response I would expect:

_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();

Note: in the above examples the HttpContext is injected into the service. The same result is achieved by using the HttpContext.Current

Update:

Doing a little more digging I see that if i write to the response stream from any controller and end the response I get a blank (0 length response) screen. Below is the index action on the home controller which causes said issue. The same code, in a throwaway project I created, works perfectly...

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

解决方案

If you change your third snippet of the code you can leave the Response.Write and Response.End out of your controller.

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(); did the trick for me. See also a nice post about this MSDN Blog about Response.End and Response.Close

这篇关于ASP.NET MVC 3 - 更换HttpContext的响应不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆