使用GZipStream压缩HTTP响应时为什么不加载CSS文件?

时间:2009-02-21 15:20:31

标签: c# compression asp.net-2.0 gzip httpcontext

我正在使用asp.net 2.0(C#)开发一个应用程序,我正在尝试实现对文件的压缩,以便提高我网站的性能。

为此,我在我的Global.asax文件中添加了一个代码来压缩所有请求(.aspx,.js,.css)但是当我运行我的应用程序时,它第一次运行良好,然后CSS没有加载网页无法正常呈现。

为什么会这样?

已编辑(已添加我的压缩代码)

我的Global.asax文件的压缩代码如下:

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;
    }

另外请告诉我是否有其他更好的方法来使用Global.asax文件,因为我没有IIS设置的访问权限,而且我也没有权限实现HttpModule,这就是我使用Global.asax文件的原因。

由于

1 个答案:

答案 0 :(得分:3)

对于静态文件,您可以配置IIS为您执行压缩,无需自己实现。

在IIS6中,这是一个全局设置(IIS管理器,服务选项卡中“网站”节点的属性)。

在IIS7中,这是基于每个文件夹设置的,它还会为您压缩动态内容。它可以在IIS管理器中设置,也可以在web.config文件中设置:

<configuration>
  <system.webServer>
    <urlCompression doDynamicCompression="true" doStaticCompression="true" />
  </system.webServer>
</configuration>