为什么使用 GZipStream 压缩 HTTP 响应时 CSS 文件无法加载?
我正在使用 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 文件的原因。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于静态文件,您可以配置IIS为您进行压缩,无需您自己实现。
在 IIS6 中,这是一个全局设置(IIS 管理器“服务”选项卡中“网站”节点的属性)。
在 IIS7 中,这是基于每个文件夹进行设置的,它还会为您压缩动态内容。 它可以在 IIS 管理器中设置,也可以在 web.xml 中设置。配置文件:
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: