MVC4捆绑GZIP和标头

时间:2012-08-27 08:09:37

标签: asp.net-mvc-4 gzip asp.net-optimization

我正在使用Google PageSpeed和YSlow测试我的网站,而我使用MVC4捆绑包创建的捆绑包没有得到

Gzipped(使用gzip或deflate压缩资源可以减少通过网络发送的字节数)并且没有

Vary:Accept-Encoding标头(指示代理服务器缓存两个版本的资源:一个压缩,一个未压缩。这有助于避免公共代理无法正确检测到Content-Encoding标头的问题。)

另外,我如何在ISS上为整个脚本文件夹添加编码头。 我知道有HTTP响应标头,然后添加自定义HTTP响应标头,

enter image description here

但这将适用于整个脚本文件夹和子文件夹以及在名称和值字段中放置的内容。

如何解决这个问题。

问候。

4 个答案:

答案 0 :(得分:11)

确保在web.config

的system.webserver部分中设置以下内容
<urlCompression doDynamicCompression="true"
                    doStaticCompression="true" dynamicCompressionBeforeCache="true" />
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge"
                   cacheControlMaxAge="365.00:00:00" cacheControlCustom="public" />
    </staticContent>

答案 1 :(得分:3)

好的Q,我简单的测试你的问题。请尝试这个逻辑:

  public HttpResponseMessage Get(){

         var request=Request.CreateResponse(HttpStatusCode.OK);
         request.Content.Headers.Add("Content-Type", "application/x-gzip");
         request.Content.Headers.Add("Content-Encoding", "gzip");

    //TODO:Add your logic here...

         return request;
    }

答案 2 :(得分:3)

要正确获取压缩的IIS上的JavaScript文件并使用GZip编码提供,请在 web.config 中添加以下内容。

 <staticContent>

      <remove fileExtension=".js" />
      <mimeMap fileExtension=".js" mimeType="text/javascript" />

      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
    </staticContent>

    <urlCompression doDynamicCompression="true" dynamicCompressionBeforeCache="true" />
    <httpCompression noCompressionForHttp10="false" noCompressionForProxies="false" dynamicCompressionDisableCpuUsage="93" dynamicCompressionEnableCpuUsage="93" staticCompressionDisableCpuUsage="99" staticCompressionEnableCpuUsage="99">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" dynamicCompressionLevel="4" />
    </httpCompression>
</system.webServer>

然后在您的ISS中使用 MIME类型

enter image description here

应用程序/ x-javascript 更改为 text / javascript

enter image description here

现在,您将在DevTools中看到JS文件在Content Encoding列中使用 gzip 提供。

enter image description here

答案 3 :(得分:1)

我认为IIS动态内容压缩应该至少要处理gzipping,甚至可能是所有这些,你尝试过这个功能吗?

This msdn article might be helpful