配置IIS 7.5以发送经过gzip压缩的JSON响应,NO_MATCHING_CONTENT_TYPE

23

所以我正在尝试让我的应用程序使用动态压缩和gzip发送JSON响应。不幸的是,这并没有起作用。服务器上所有的静态压缩都很好,但动态压缩却没有。

我通过添加以下内容来设置:

<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
将以下内容添加到应用程序域.config文件中<httpCompression><dynamicTypes>部分。我使用Charles来检查HTTP请求,并且可以验证我发送请求时设置了Accept-Encoding:gzip,deflate头。我尝试过使用Accept:* / *Accept:application / json两种方式。当无法工作时,我启用了“失败请求”跟踪日志,以查找DYNAMIC_COMPRESSION_NOT_SUCCESS的错误代码,这是NO_MATCHING_CONTENT_TYPE
我一直在论坛和谷歌上进行研究,但我看到的都是人们说使用带有指定字符集的mimeType可以修复他们的问题,但在我的情况下仍然无法工作,我可以验证响应返回一个标头,其中包含Content-Type:application / json; charset = utf-8 提供JSON响应的端点是使用[ScriptService()]类注释的标准.NET ASMX WebServices,并在方法上使用 [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)] 进行注释。 它们可以正常返回 JSON,但我却无法让动态压缩正常工作。
由于这些也是常规的 web 方法,因此我还添加了:
<add mimeType="text/xml" enabled="true" />
<add mimeType="text/xml; charset=utf-8" enabled="true" />

尝试对XML响应进行gzip压缩。令人沮丧的是,使用相同方法发送JSON时,这种压缩不起作用。目前我有点茫然。

1个回答

36
请确保在添加类型之后,*/* MIME类型已经存在。另外,请确保您已使用服务器管理器(或OptionalFeatures.exe)安装了动态压缩模块。
以下是我用来确保良好压缩的命令行。(但请确保您实际上已经安装了动态和静态压缩模块):
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/serverRuntime /frequentHitThreshold:"1"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/urlCompression /doDynamicCompression:"True"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"dynamicTypes.[mimeType='application/json']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"dynamicTypes.[mimeType='application/json; charset=utf-8']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"dynamicTypes.[mimeType='application/javascript']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/javascript',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"dynamicTypes.[mimeType='application/x-javascript']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/x-javascript',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"dynamicTypes.[mimeType='application/x-javascript; charset=utf-8']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/x-javascript; charset=utf-8',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"dynamicTypes.[mimeType='*/*']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='*/*',enabled='False']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"staticTypes.[mimeType='application/javascript']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/javascript',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"staticTypes.[mimeType='application/x-javascript']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/x-javascript',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"staticTypes.[mimeType='application/x-javascript; charset=utf-8']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/x-javascript; charset=utf-8',enabled='True']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-"staticTypes.[mimeType='*/*']"
call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='*/*',enabled='False']"

call %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /noCompressionForHttp10:"False" /noCompressionForProxies:"False" /minFileSizeForComp:"2700"

运行此命令后,您的 %windir%\system32\inetsrv\config\ApplicationHost.config 文件应该如下所示(请注意/在底部):
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" minFileSizeForComp="2700" noCompressionForHttp10="false" noCompressionForProxies="false">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <dynamicTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/json; charset=utf-8" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/x-javascript; charset=utf-8" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/x-javascript; charset=utf-8" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>

谢谢,实际上我自己一段时间前就解决了这个问题,但是我不记得我是怎么做的,而且我已经离开了当时所在的公司。我现在相信是使用一个带有这些命令的脚本,并将其集成到应用程序部署的MSBuild定义中。 - Sloloem
2
我花了一整天的时间在谷歌上搜索为什么压缩不起作用。出于某种原因,只有运行你的脚本才解决了我的问题(直接在apphost文件中设置并没有解决)。谢谢! - LoremFooBar
为什么您在静态和动态部分都有相同的 MIME 类型? - Doug S
这些脚本调用今天真的救了我的命。实际上,我已经在appHost.config中有了那个部分...但是当我应用脚本的每一行时,它都会说找不到元素。所以这修改了其他东西,不确定是哪里,但它起作用了! - Stuart Allen
这是我的解决方法:添加以下内容:<add mimeType="application/json; charset=utf-8" enabled="true" /> - Uğur Dinç
@carlos-aguilar-mares 你知道我们如何实现在特定的web.config文件中添加httpcompression和mime吗? - Shashank Sood

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