ASP.NET MVC 3 - Replacing HttpContext Response not working

I am using Nopcommerce, which has recently been updated to use MVC3 - previously it used web forms. I am trying to connect to the Worldpay website (payment gateway). The process is a bit confusing, but essentially the form has to be submitted to Worldpay, and the user is then redirected to his hosted site to fill in his credit card details, etc.

Nopcommerce will take care of this using the Post method, which on the server side creates a form to be published and replaces the httpcontext response with embedded 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 works correctly. However, processing continues, and execution returns from the service level (where this method is located) to the controller, and the following redirection is issued:

  return RedirectToRoute("CheckoutCompleted"); 

Does MVC work differently with web forms regarding server side response changes? I tried the following code and can confirm that worldpay really hits and sends the answer 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 using HttpContext.Current

Update:

By doing a bit more digging, I see that if I write to the response stream from any controller and end the response, I get a blank (0 long answer) screen. The following is the index action on the home controller that causes this problem. The same code in the project I created works fine ...

  public ActionResult Index() { Response.Write("test"); Response.End(); return View(_storeInformationSettings); } 
+4
source share
1 answer

If you change your third piece of code, you can leave Response.Write and Response.End from 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 (); helped. See also a good post on this MSDN Blog about Response.End and Response.Close.

0
source

Source: https://habr.com/ru/post/1390167/


All Articles