Web API Gzip未被应用

Fir*_*ion 18 c# gzip asp.net-web-api2

我添加了web.config条目以启用基于此S/O答案启用IIS7 gzip的 gzip压缩.

然后我在加载ASPX页面时检查了Chrome Developer窗口,并在响应中看到了标题:

Cache-Control:private
Content-Encoding:gzip
Content-Length:3669
Content-Type:text/html; charset=utf-8
Date:Wed, 04 Mar 2015 00:46:05 GMT
Server:Microsoft-IIS/7.5
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)

这意味着它"正常",对吗?但是在进行Web API调用时查找该标头时,它不存在:

Cache-Control:no-cache
Content-Length:551
Content-Type:application/json; charset=utf-8
Date:Wed, 04 Mar 2015 00:53:05 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)

我尝试了各种不同的配置(从上面链接的S/O答案中推荐的配置开始).最后,在绝望的行为中,我将其设置为此,我认为它会尝试压缩所有请求(除了*/*注释掉的所有内容):

  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
      <add mimeType="*/*" enabled="true"/>
      <!--<add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="application/json" enabled="true"/>-->
      <!--<add mimeType="*/*" enabled="false"/> -->
    </dynamicTypes>
    <staticTypes>
      <add mimeType="*/*" enabled="true"/>
      <!--<add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="application/json" enabled="true"/>-->
      <!-- add mimeType="*/*" enabled="false"/>-->
    </staticTypes>
  </httpCompression>
  <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
Run Code Online (Sandbox Code Playgroud)

什么可以阻止GZIP应用于我的Web API方法?

更新

我已经尝试过NuGet Web API压缩包,并在IIS Express 8.0(Visual Studio)和本地安装的IIS 7.5中编辑applicationHost.config.所有都产生了相同的结果:对text/*等其他mime类型的请求工作,但是application/json拒绝被gzip压缩.

Pau*_*aul 11

WebAPI是否位于防火墙,Web代理,病毒防护套件之后?正如在更快的网站中所提到的:Web开发人员的性能最佳实践作者:Steve Souders 这可能会剥离标题.

  • 谢谢 - 对我来说,ESET AntiVirus在内部解压缩所有响应.解决方案是禁用"Web扫描程序"模块. (4认同)
  • 好吧,事实证明我错了.在同一台开发机器上,有一个完整的IIS安装正在运行应用程序请求路由,它充当反向代理,允许来自本地环境之外的计算机命中_that_ IIS,然后IIS将请求转发到本地IIS Express实例(不允许外部连接).尽管我在尝试启用gzip时对本地实例进行了大部分测试,但完整IIS中的路由条目位于web.config(同一目录)中.删除该web.config条目使NuGet gzip生效. (2认同)

小智 5

根据ASP.NET Web API 压缩(Ben Foster 博客),您有两种选择:

  1. 更改您的applicationHost.config并添加

    到 httpCompression -> dynamicTypes 部分。

  2. 在您的 web api 管道中使用委托处理程序来处理压缩。
    例如Fabrik.CommonMicrosoft ASP.NET Web API 压缩支持


alv*_*alv 5

多亏了上述 2 个解决方案和其他地方的其他解决方案,我想逐步解释如何使用 Web API 2.2 进行 http 压缩可能会有所帮助,因为自上述帖子以来,一些包/命名空间发生了变化。

1)使用nuget包管理器控制台安装以下内容;

Install-Package Microsoft.AspNet.WebApi.MessageHandlers.Compression
Run Code Online (Sandbox Code Playgroud)

2)在WebApiConfig.cs里面添加这些usings;

using System.Net.Http.Extensions.Compression.Core.Compressors;
using Microsoft.AspNet.WebApi.Extensions.Compression.Server;
Run Code Online (Sandbox Code Playgroud)

3) 在WebApiConfig.cs里面添加Register(HttpConfiguration config)的底部;

GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
Run Code Online (Sandbox Code Playgroud)

4) 编辑你的 web.config 并在 system.webServer 里面添加;

<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="true" />
<httpCompression>
    <dynamicTypes>
        <clear />
        <add enabled="true" mimeType="text/*" />
        <add enabled="true" mimeType="message/*" />
        <add enabled="true" mimeType="application/x-javascript" />
        <add enabled="true" mimeType="application/javascript" />
        <add enabled="true" mimeType="application/json" />
        <add enabled="false" mimeType="*/*" />
        <add enabled="true" mimeType="application/atom+xml" />
    </dynamicTypes>
    <staticTypes>
        <clear />
        <add enabled="true" mimeType="text/*" />
        <add enabled="true" mimeType="message/*" />
        <add enabled="true" mimeType="application/javascript" />
        <add enabled="true" mimeType="application/atom+xml" />
        <add enabled="true" mimeType="application/xaml+xml" />
        <add enabled="true" mimeType="application/json" />
        <add enabled="false" mimeType="*/*" />
    </staticTypes>
</httpCompression>
Run Code Online (Sandbox Code Playgroud)

第一次在本地和 azure 网站上工作,所以希望它对你有用!另外当然不需要弄乱applicationHost.config ...