views:

7463

answers:

8

Hi,

I am trying to implement GZip compression for my asp.net page (including my CSS and JS files). I tried the following code, but it only compresses my .aspx page (found it from YSlow)

HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;

The above code is only compressing my .aspx page code (markup) not the CSS and JS files which is included as external files. Please tell me how can I implement GZip compression in ASP.NET using code (because I am on shared hosting server where I don't have access to IIS Server configurations). And also in the above code I am not getting the last two lines, why they are used and what's the purpose of these lines. Please explain!

Thanks

+9  A: 

For compressing JS & CSS files you actually have to handle that at the IIS level, since these files are rendered directly without the ASP.NET runtime.

You could make a JSX & CSSX extension mapping in IIS to the aspnet_isapi.dll and then take advantage of your zip code, but IIS is likely to do a better job of this for you.

The content-encoding header tells the browser that it needs to unzip the content before rendering. Some browsers are smart enough to figure this out anyway, based on the shape of the content, but it's better to just tell it.

The Accept-encoding cache setting is there so that a cached version of the gzipped content won't be sent to a browser that requested only text/html.

Ben Scheirman
Hi @Ben, Can you please tell me how to compress my files using IIS, what all settings I have to perform, Although I don't have access to IIS configurations, but I'll try to do it. Please tell me how to compress files using IIS ?? Thanks!
Prashant
+1  A: 

To answer your last question. Those two lines set HTTP headers for the response sent back to the browser. Content-Encoding tells the browser that the response is encoded as gzip and it needs to be decoded. The last line adds Accept-Encoding to the Vary header. With this the browser or proxies can determine whether the response was unique or is influenced by certain other headers and adjust their caching.

gix
+1  A: 

For your first question: http://msmvps.com/blogs/omar/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx says it better than I can myself,

And he supplies downloadable code too...

Stobor
I am using ASP.NET 2.0 Can i use this sample in my application. Or Do I need ASP.NET MVC to use this sample ???
Prashant
+5  A: 

This is a good link on compressing dynamic/static content and showing you how to verify using fiddler.

HTTP COMPRESSION in IIS 6 and IIS 7

Gulzar
+2  A: 

In IIS7 all requests go to .net, so you would have to create an HttpModule that added those headers to all responses.

Without IIS7, and on shared hosting, you would have to creare a handler that mapped a .net file extention that you are not using (like .asmx) and in the web.config specify that .asmx files go to your HttpHandler which is set to rewrite the path to .jpg or whatever and set the header there too.

rizzle
+2  A: 

The reason it's only compressing your ASPX file is that the code you have written is only embedded in the ASPX file. An ASPX file is a separate request from any linked content it contains. So if you have an ASPX page that contains:

<img src="www.example.com\exampleimg.jpg" alt="example" />

This would amount to 2 requests (DNS lookups aside) from your browser to the resources:

  1. for the aspx page and
  2. for the image contained by the aspx page.

Each request has it own response steam. The code you have posted is attaching to the ASPX response stream only, which is why only your ASPX page is being compressed. Lines 1 & 2 of your posted code are essentially taking over the normal response stream of the page and injecting some "middle man" code that in this case eats and compresses the normal output stream with a GZip stream and sends that down the wire instead.

Lines 3 & 4 are setting up the response headers. All http requests and responses have headers that are sent prior to the content. These set up the request/response so that the server and client know what is being sent and how.

In this case Line 3 is informing the client browser that the response stream is compressed via gzip and therefore needs to be de-compressed by the client browser prior to display.

And Line 4 is simply turning on the Accept-Encoding header of the response. This would otherwise have been absent from the response.

There are pluggable modules you can write/obtain that allow you to compress a multitide of other MIME type such as *.js and *.css but you're better off just using the built in compression functionality of IIS.

You haven't said which verson of IIS you are using but if it were IIS 7.0, it would require that you include something like the following into the section of you web.config file:

<httpCompression> 
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" /> 
 <staticTypes>
         <add mimeType="text/*" enabled="true" />
      </staticTypes>
</httpCompression> 
<urlCompression doStaticCompression="true" /> 
intermension
A: 

Hi, Check this out, maybe it will be handy: http://helicontech.blogspot.com/2009/03/guide-how-to-enable-modgzip-compression.html

Slava
+1  A: 

If you do not have the access to IIS, for example, you are using shared hosting, you may refer to the article below on how to enable Gzip using Web.config.

How to Gzip on ASP.net and GoDaddy

I have tried it and it works on Godaddy Shared Windows Hosting.

Regards, JeeShen Lee.

JeeShen Lee