The ASP.NET Capsule #24: Examine Output Compression

Hi all.

After an interesting discussion with Cristian Prieto (ASP.NET MVP) he pointed me in the right direction of a problem I was having.

The issue was with a web application I recently published to IIS. A new page I added was throwing an exception but instead of showing the error as I would expect, it was throwing a lot of garbage:

image

Now, the problem was that I didn’t have a way to find what the real problem was until he said that the output was probably being compressed, so after following his advice I checked with Firebug and the results were:

compressed_output

If you see the header Content-Encoding the value is gzip. That was a clear indicator of the output being compressed.

I checked the IIS7 settings and compression was not enabled, so I checked the Global.asax file and this is what I found:

   1: protected void Application_BeginRequest(object sender, EventArgs e)

   2: {

   3:     if (Request.RawUrl.Contains(".aspx") && (Request.Headers["Accept-Encoding"] != null))

   4:     {

   5:         if (Request.Headers["Accept-Encoding"].ToLower().Contains("gzip"))

   6:         {

   7:             Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, CompressionMode.Compress, true);

   8:             Response.AppendHeader("Content-Encoding", "gzip");

   9:         }

  10:     }

  11: }

After a couple of small refactorings thanks to ReSharper:

   1: protected void Application_BeginRequest(object sender, EventArgs e)

   2: {

   3:     if (!Request.RawUrl.Contains(".aspx") || (Request.Headers["Accept-Encoding"] == null)) return;

   4:     if (!Request.Headers["Accept-Encoding"].ToLower().Contains("gzip")) return;

   5:     Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, CompressionMode.Compress, true);

   6:     Response.AppendHeader("Content-Encoding", "gzip");

   7: }

I was now able to disable output compression and see what the issue was. After solving the problem I enabled it again and things work as expected.

Checkout Cristian’s blog post about output compression (in Spanish).

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *