ASP.Net - Using gzip results in corruption when retrieved from cache
<p>Using ASP.Net 2.0 and IIS 6</p>

<p>I enabled compression by extending IHttpModule and registering in the Web.config file</p>

<pre><code>public class EnableCompression : IHttpModule
{

    public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }

    private void Application_BeginRequest(Object source, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        String encoding = context.Request.Headers.Get("Accept-Encoding");

        if (encoding == null)
            return;

        encoding.ToLower();

        if (encoding.Contains("gzip"))
        {
            context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
        }
        else
        {
            context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
            HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
        }
    }

    void IHttpModule.Dispose()
    {
        throw new Exception("The Method or Operation is not Implemented");
    }

}
</code></pre>

<p>The above works just fine until I also set an expires header in the page load event on my master page code file</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.SetLastModified(DateTime.Now);
} 
</code></pre>

<p>After setting the expires header, I get a bunch of garbage once the browser retrieves data from cache. So the first time a page is loaded, it's fine. If I go to a new page and then got back to the first page, I only get a page full of:</p>

<pre><code>��`I�%&amp;/m�{J�J��t��`$ؐ@�����iG#)�*�
</code></pre>

<p>If I disable compression but leave the expires header, it's fine
If I disable the expires header but leave compression enabled, it's fine
If I enable both, I get a page full of garbage.</p>

<p>I have no idea what's going on. </p>

<pre><code>Response Header
HTTP/1.1 200 OK
Date: Sun, 08 Jan 2012 07:23:15 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Content-Encoding: gzip
Cache-Control: private
Expires: Sun, 08 Jan 2012 07:33:00 GMT
Last-Modified: Sun, 08 Jan 2012 07:23:00 GMT
Vary: *
Content-Type: text/html; charset=utf-8
Content-Length: 13613

Request Header
GET /eng/DE/ HTTP/1.1
Host: wwwnpg
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: ASP.NET_SessionId=gxdxwnnum5rumue13rxmc5mb
</code></pre>
