我正在尝试为我的 asp.net 页面(包括我的 CSS 和 JS 文件)实现 GZip 压缩。我尝试了以下代码,但它只压缩我的 .aspx 页面(从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;
上面的代码仅压缩我的 .aspx 页面代码(标记),而不压缩作为外部文件包含的 CSS 和 JS 文件。
请告诉我如何使用代码在 ASP.NET 中实现 GZip 压缩(因为我在共享托管服务器上,无法访问 IIS 服务器配置)。而且在上面的代码中,我没有得到最后两行,为什么使用它们以及这些行的目的是什么。请解释!
这是 css 和 javascript 文件的解决方案。将以下代码添加到 web.config 文件内的 中:
<configuration>
...
<system.webserver>
...
<httpCompression>
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
...
</system.webserver>
...
<configuration>
Credit: 如何在 ASP.NET 和 GoDaddy 上进行 GZip
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)