如何在ASP.NET中实现GZip压缩? [英] How to implement GZip compression in ASP.NET?

查看:403
本文介绍了如何在ASP.NET中实现GZip压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的asp.net页面(包括我的CSS和JS文件)实现GZip压缩。我试过下面的代码,但它只压缩我的.aspx页面(从找到它YSlow ) / p>

  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;上面的代码只压缩我的.aspx页面代码(标记),而不是压缩CSS和JS文件。作为外部文件。
请告诉我如何在ASP.NET中使用代码实现GZip压缩(因为我在共享托管服务器上,我没有访问IIS服务器配置)。并且在上面的代码中,我没有得到最后两行,为什么他们被使用,这些行的目的是什么。请解释!



感谢

解决方案

CSS文件,你实际上必须在IIS级别处理,因为这些文件是直接渲染的ASP.NET运行时。



你可以做一个JSX& CSSX扩展映射在IIS到aspnet_isapi.dll,然后利用你的邮政编码,但IIS可能会为你做一个更好的工作。



内容-encoding标头告诉浏览器它需要在渲染之前解压缩内容。有些浏览器很聪明,无论如何,基于内容的形状,但最好只是告诉它。这是非常聪明的。



接受编码缓存设置是有因此缓存版本的gzip压缩内容将不会发送到只请求text / html的浏览器。


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

解决方案

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.

这篇关于如何在ASP.NET中实现GZip压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆