配置IIS 7.5以发送jzipped的JSON响应,NO_MATCHING_CONTENT_TYPE

时间:2012-07-23 17:49:40

标签: .net json iis-7 http-compression

所以我试图让我的应用程序使用动态压缩和gzip发送其JSON响应。不幸的是,这不起作用。服务器上的所有静态压缩都运行正常,但不是动态的。

我通过添加:

来设置它
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />

到applicationHost.config文件中<dynamicTypes>的{​​{1}}部分。我正在使用Charles检查HTTP请求,我可以验证我是否使用<httpCompression>标头集发送请求。我尝试了Accept-Encoding: gzip, deflateAccept: */*。当它无法工作时,我启用了“失败请求”跟踪日志记录,以查找Accept: application/json的错误代码,即DYNAMIC_COMPRESSION_NOT_SUCCESS

我一直在尝试研究论坛和谷歌,但我所能看到的是人们说使用指定的字符集的mimeType修复了他们的问题,但在我的情况下,它仍然无法正常工作,我可以验证响应返回时标题为NO_MATCHING_CONTENT_TYPE

为JSON响应提供服务的端点是标准的.NET ASMX WebServices,在类中使用Content-Type: application/json; charset=utf-8注释,在方法中使用[ScriptService()]注释。他们返回JSON很好,但我不能让动态压缩为我的生活工作。

由于这些是常规网络方法,我还添加了:

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

尝试gzip XML响应。令人沮丧的是,这种压缩在从同一方法发送JSON时不起作用。在这一点上,我有点不知所措。

1 个答案:

答案 0 :(得分: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>
相关问题