如何在ASP.NET中启用Gzip压缩?

3

我希望能在网站中使用gzip压缩css和js文件,并在web.config中使用以下代码:

<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
  <dynamicTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>

使用gtmetrix.com网站分析我的网站,但是收到以下信息:
Compressing the following resources with gzip could reduce their transfer size by 63.4KiB (77% reduction).

Compressing http://mysite.com/ could save 30.6KiB (74% reduction).
Compressing http://mysite.com/WebResource.axd?d=TdZvTxV1RZCMol6igoEA7XxfdqcWpTRlYuCxfymg_ruU4xc1NuSqLn7bduKh3WyPwSYX0g86d6fEJ3SVBdeWi5FBnMpKfcO7aaFoqvmTo_c1&t=634970873020000000 could save 17.5KiB (80% reduction).
Compressing http://mysite.com/main.css could save 14.8KiB (80% reduction).
3个回答

1
根据这个答案,压缩发生在后台进程的不同线程上,因此在第一次请求时不可用。此外,请记住,这不是ASP.NET功能,而是IIS功能。
还有一个答案在这里显示了有效的配置。
提示:您可能还想检查使用捆绑和缩小

0

对于 Web.config 中的动态类型,您还需要在 IIS 中安装动态压缩模块,否则这些内容将不起作用。


-1

我找到了一段代码,你应该在 web.config 文件中使用它,在你的文件管理器中:

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
  <dynamicTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </staticTypes>
</httpCompression>

我现在在我的网站www.irpolymer.com中使用它,但在gtmetrix中显示我的gzip被禁用,我认为明天会修复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接