我遇到了同样的问题,因此使用了使用 global.asax 调用的 .net 压缩以及使用 .aspx 扩展名重命名静态 css 并通过 Web.config 使用 url 重写在 .css.aspx 顶部我只是添加了
<%@ Page ContentType="text/css" %>
在web.config配置的system.webserver重写规则中:
<rule name="san aspx">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.aspx" />
</rule>
<rule name="revert aspx">
<match url="(.*)\.aspx$"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
</rule>
在 global.asax 的 Application_PreRequestHandlerExecute 中
HttpApplication app = sender as HttpApplication;
string acceptEncoding = app.Request.Headers["Accept-Encoding"];
Stream prevUncompressedStream = app.Response.Filter;
if (acceptEncoding != null && acceptEncoding.Length > 0) {
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains("gzip")){
app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
}
}
Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
它看起来很吓人,但通过建立正确的理解,它就像魅力一样,值得节省与 PageSpeed 相关的 SEO 以及共享托管。我确实尝试过我的托管服务提供商的支持,但他们只是推销了更多奇特的托管包。对于像 .svgz 文件这样的强制压缩内容,我使用了单独的文件夹和该文件夹的 web.config 的 system.webserver 的 httpProtocol 我写道:
<customHeaders>
<add name="Content-Encoding" value="gzip" />
</customHeaders>