我正在做一些挑剔的seo,我担心firebug显示我的捆绑和minifed css文件没有压缩,我使用动作过滤器来附加gzip标头,但我不知道为什么它不适用于缩小/ bundled css和js文件?
这是过滤器:
public class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding)) return;
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
response.AppendHeader("Connection", "keep-alive");
}
}
我在Application_Start()中注册它,所以我不必在每个控制器上执行它:
GlobalFilters.Filters.Add(new CompressFilter());
RegisterGlobalFilters(GlobalFilters.Filters);
我甚至在配置中有这个:
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
标题对于视图是正确的,但css和js没有。我该如何解决?
答案 0 :(得分:0)
如果您不使用IIS 7压缩,则不需要CompressFilter,urlCompression设置应该足够了。如果您的css是通过ScriptResourceHandler提供的,那么您也应该为此设置压缩(请注意,在此示例中启用了缓存):
<configuration>
...
<system.web.extensions>
<scripting>
<scriptResourceHandler enableCompression="true" enableCaching="true"/>
</scripting>
</system.web.extensions>
...
</configuration>
只是注意,ISA Proxy服务器默认从Accept-Encoding请求头中剥离gzip,因此ISA可以扫描内容中的恶意数据,也许其他一些代理/防火墙软件会做同样的事情。