我正在做一些挑剔的 seo,我担心萤火虫显示我的捆绑和缩小的 css 文件没有被压缩,我正在使用动作过滤器来附加 gzip 标头,但我不知道为什么它不适用于缩小/捆绑的 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 是正确的。我该如何解决这个问题?