Relaying an HttpWebResponse in ASP.NET MVC

I have recently been involved in building an ASP.NET MVC3 wrapper around a PHP web application primarily for security purposes.  A whole bunch of GET, POST and ajax requests had to be proxied between the MVC site and the internal site on the network.  Although this doesn’t sound too complicated, each request required cookie authentication and involved a range of content types (HTML, JS, CSS, JSON & images).  I ended up subclassing System.Web.Mvc.ActionResult to take a System.Net.HttpWebResponse as a parameter, and relay it back to the client as faithfully as possible.  In processing the HttpWebResponse, it decompresses the response stream, removes the Transfer-Encoding, Content-Length and Content-Encoding headers (these are handled by IIS), copies across the rest of the headers and initialises a ContentResult or FileStreamResult to pass back the response.

You can download the solution with an example site here, or read through the source below. I also have a blog on relaying requests.

Usage:

 

Source: