views:

882

answers:

1

I am developing an application using asp.net 2.0 (C#), in which I am trying to implement the compression of my files, so that performance of my website will improve.

For that I have added a code in my Global.asax file to compress all requests (.aspx, .js, .css) But when I am running my application it works well for first time then the CSS is not loading and web page is not rendering properly.

Why its happening??

Edited (added my compression code)

My compression code of Global.asax file is as follows:

void Application_BeginRequest()
    {
        HttpContext incoming = HttpContext.Current;
        string oldpath = incoming.Request.Path.ToLower();

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

Also please let me know if there is any other better way to do the same, using the Global.asax file, because I don't have access of IIS Settings and also I don't have permission to implement the HttpModule, that is why I am using Global.asax file.

Thanks

+2  A: 

For static files, you can configure IIS to do the compression for you, no need to implement it yourself.

In IIS6 this is a global settings (properties of the "Web Sites" node in IIS manager, service tab).

In IIS7 this is set on a per folder basis, and also it will compress dynamic content for you. In the case of IIS7 it can either be set my the IIS Manager or by the web.config file:

<configuration>
  <system.webServer>
    <urlCompression doDynamicCompression="true" DoStaticCompression="true" />
  </system.webServer>
</configuration>
Richard
I can't caz I don't have access to IIS settings as I am on Shared Hosting server.
Prashant
In IIS7 you will be able to set this in a web.config file (all IIS7 settings are just web.config entries). For IIS6 that would be a problem. Which version of IIS are you using?
Richard
I am using IIS7, please tell me what settings I have to add in we.config file. Please ??
Prashant
Done, the link takes you to the reference for <system.webServer> which is the section used for IIS7 configuration not relating directly to ASP.NET.
Richard
Yups, done. But in the urlCompression tag "doDynamicCompression" is false. When I am setting it truw then My pages are gzipped, else they are not.
Prashant
Also on the link you have given a note is written which says: "Note: Use of dynamic compression may increase processor utilization and reduce the overall performance of the Web server."
Prashant
But as far as I know its recommended that you should compress (http://developer.yahoo.com/performance/rules.html#gzip) your contents before serving them. Then why that not is given there.
Prashant
IIS does cache the static files once compressed (the size and location of this cache is set at the server level).
Richard
The performance trade off cannot be answered in general, except that with CPU growing faster than memory and network bandwidth, compression overheads are being less significant. Only you can make this trade off.
Richard