【问题标题】:gzip compression doesn't work and can't get 304 in chromegzip 压缩不起作用,无法在 chrome 中获得 304
【发布时间】:2018-10-27 10:32:49
【问题描述】:

我正在我的 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: 每个文件的分块,而不是 gzip。 这个过滤器是从我以前的项目中复制而来的,预计它仍然可以工作。会不会是 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

【问题讨论】:

    标签: asp.net-mvc google-chrome caching compression gzip


    【解决方案1】:

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

    F5/If-Modified-Since 问题。

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

    chunked/gzip 问题

    清除浏览器缓存并检查第一个响应。 Fiddler 将显示“响应正文已编码”,表示已压缩(gzip 或 deflate)。

    您是否看到 Transfer-Encoding 分块取决于 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 静态文件模块来处理资产服务,所以您必须自己处理所有响应标头。

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2018-04-19
      • 2015-10-10
      相关资源
      最近更新 更多