Compress Filter + MVC + Yahoo YSlow

2019-04-29 21:54发布

问题:

I've been using Yahoo's YSLOW to try and make my pages go faster at AgentX

I am using the below compress filter. When I run the site through visual studio YSLOW says that all the files are compressed and I get an A when I view the live site it gets an E and says my files need to be gzipped. Can anyone explain?

    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);
        }
    }
}

回答1:

I use the same mechanism on my site:

http://www.avantprime.com/articles/view-article/7/compress-httpresponse-for-your-controller-actions-using-attributes

I suggest using fiddler to see if your response from the live site is actually compressed and then you can determine if there is something up with YSlow or with your code.

I suggest that you run google pagespeed also http://code.google.com/speed/page-speed/. This does the same job as YSlow but made by google. Different algorithms for some things.

DaTribe



回答2:

Are you running Production on IIS 6? Perhaps this can help. ASP.NET MVC compression options in IIS6