繁体   English   中英

gzip压缩不起作用,并且无法在chrome中获得304

[英]gzip compression doesn't work and can't get 304 in chrome

我正在我的asp.net mvc 5应用程序中使用压缩和缓存机制。

我正在发送带有以下缓存标头的文件:

            Response.Cache.SetCacheability(HttpCacheability.Public);
            Response.Cache.SetExpires(DateTime.UtcNow.AddYears(1).ToUniversalTime());
            Response.Cache.SetLastModified(System.IO.File.GetLastWriteTime(serverPath).ToUniversalTime());
            Response.AppendHeader("Vary", "Accept-Encoding");

IE11,Edge,Firefox,都在F5刷新时发送If-Modified-Since标头,但不是Chrome。 为什么这样以及如何解决它? 在Chrome中,我获得了200个状态代码,文件从缓存中加载。

我遇到的第二个问题是启用gzip压缩。 我有一个标准的动作过滤器:

public class CompressContentMvcAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            GZipEncodePage();
        }

        private bool IsGZipSupported()
        {
            string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

            if (!string.IsNullOrEmpty(AcceptEncoding) &&
                    (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate")))
            {
                return true;
            }

            return false;
        }

        private void GZipEncodePage()
        {
            HttpResponse Response = HttpContext.Current.Response;

            if (IsGZipSupported())
            {
                string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

                if (AcceptEncoding.Contains("gzip"))
                {
                    Response.Filter = //new GZipCompressionService().CreateCompressionStream(Response.Filter);
                        new System.IO.Compression.GZipStream(Response.Filter,
                                                System.IO.Compression.CompressionMode.Compress);
                    Response.Headers.Remove("Content-Encoding");
                    Response.AppendHeader("Content-Encoding", "gzip");
                }
                else
                {
                    Response.Filter =// new DeflateCompressionService().CreateCompressionStream(Response.Filter);
                        new System.IO.Compression.DeflateStream(Response.Filter,
                                                System.IO.Compression.CompressionMode.Compress);
                    Response.Headers.Remove("Content-Encoding");
                    Response.AppendHeader("Content-Encoding", "deflate");
                }
            }

            // Allow proxy servers to cache encoded and unencoded versions separately
            Response.AppendHeader("Vary", "Content-Encoding");
        }
    }

我在我的操作方法上应用此过滤器返回应用程序资产,但它获得了Transfer-Encoding:为每个文件分块,而不是gziped。 这个过滤器是从我以前的项目中复制过来的,它仍在工作中。 这可能是IIS服务器的问题吗? 本地我有一个IIS 10和.NET 4.7,它的旧应用程序,它的工作原理是在IIS 8.5和框架4.5上托管。 想不出别的什么。 我在第二天谷歌搜索,找不到任何线索。 我对IIS中的压缩不感兴趣。

[编辑]

我从回复得到的标题:

 HTTP/1.1 200 OK
Cache-Control: public
Content-Type: text/javascript
Expires: Sat, 18 May 2019 08:58:48 GMT
Last-Modified: Thu, 10 May 2018 13:26:02 GMT
Vary: Content-Encoding
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 18 May 2018 08:58:48 GMT
Transfer-Encoding: chunked

我总是用Fiddler来检查这些挑战。

F5 / If-Modified-Since问题。

如果已设置过期标头且其日期时间值仍为实际值,Chrome就不会发出新请求。 因此Chrome尊重您预期的缓存行为。 通过其他浏览器浏览您的网站时,您会发现这些资产也不会发送任何请求。 F5是'特殊',它是强制刷新。

chunked / gzip问题

清除浏览器缓存并检查第一个响应。 Fiddler将显示“Response body is encoded”,表示压缩(gzip或deflate)。

是否看到Transfer-Encoding chunked取决于Content-Length头是否存在。 请参阅下面的回复中的差异。 如果您不希望分块的Transfer-Encoding设置Content-Length标头。

Content-Type: text/javascript; charset=utf-8
Content-Encoding: gzip
Expires: Sat, 25 May 2019 13:14:11 GMT
Last-Modified: Fri, 25 May 2018 13:14:11 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0
Content-Length: 5292

Content-Type: text/javascript; charset=utf-8
Transfer-Encoding: chunked
Content-Encoding: gzip
Expires: Sat, 25 May 2019 13:14:11 GMT
Last-Modified: Fri, 25 May 2018 13:14:11 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0

因为您通过自己的代码处理资产的服务,而不是通过IIS静态文件模块,所以您必须自己处理所有响应头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM