Question

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

Was it helpful?

Solution

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 setting (properties of the "Web Sites" node in IIS manager, service tab).

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

<configuration>
  <system.webServer>
    <urlCompression doDynamicCompression="true" doStaticCompression="true" />
  </system.webServer>
</configuration>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top