Jose R. Guay Paz

Most of the time on
ASP.NET, C# & SQL Server

Sponsors

News

INETA Community Speakers Program

My latest tweets

this.blog.owner=

  • CSW Solutions
  • Follow Me
  • Works On My Machine

.NET Communities

  • INETA Latam

Blogs I Read

Websites I like

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!



Comments

Jeffrey said:

Is there any difference between the 2 pieces of code? I think they are the same and perfectly correct. How did you fixed the problem?

# September 24, 2009 2:04 PM

Jose Rolando Guay Paz said:

@Jeffrey: Yes, they are both correct. The second one is after it was refactored by ReSharper, that's the only difference. To solve my problem I comented the compression code so I could actually see what the real problem was. Once fixed, turned compression on again by uncomment the code.

# September 24, 2009 2:54 PM

Writing Services said:

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.

# August 18, 2010 6:13 AM

Chiropractor Hoffman Estates said:

This is looking special characters.

# February 14, 2011 2:22 AM

arrhythmia treatment said:

This is some thing unique thanks for sharing

# March 3, 2011 6:12 AM