【问题标题】:Configuring IIS 7.5 to send JSON responses gzipped, NO_MATCHING_CONTENT_TYPE配置 IIS 7.5 以发送压缩后的 JSON 响应,NO MATCHING CONTENT_TYPE
【发布时间】:2012-07-21 23:55:02
【问题描述】:

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

我已经通过添加:

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

到 applicationHost.config 文件中 &lt;httpCompression&gt;&lt;dynamicTypes&gt; 部分。我正在使用 Charles 来检查 HTTP 请求,并且可以验证我正在发送带有 Accept-Encoding: gzip, deflate 标头集的请求。我试过Accept: */*Accept: application/json。当它不起作用时,我启用了“失败的请求”跟踪日志来查找 DYNAMIC_COMPRESSION_NOT_SUCCESS 的错误代码,即 NO_MATCHING_CONTENT_TYPE

我一直在尝试在论坛和 Google 上进行研究,但我所看到的只是人们说使用带有指定字符集的 mimeType 可以解决他们的问题,但在我的情况下它仍然无法正常工作,我可以验证响应返回一个标题为Content-Type: application/json; charset=utf-8

提供 JSON 响应的端点是标准的 .NET ASMX WebServices,在类上用 [ScriptService()] 注释,在方法上用 [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 注释。他们返回 JSON 很好,但我无法让动态压缩为我的生活工作。

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

<add mimeType="text/xml" enabled="true" />
<add mimeType="text/xml; charset=utf-8" enabled="true" />

尝试对 XML 响应进行 gzip 压缩。令人沮丧的是,这种压缩有效,而从同一方法发送 JSON 则无效。在这一点上,我有点不知所措。

【问题讨论】:

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


    【解决方案1】:

    您要确保*/* 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 定义中。
    • 我花了一整天的时间在谷歌上搜索为什么压缩对我不起作用。出于某种原因,只有运行你的脚本才能解决我的问题(直接在 apphost 文件中设置没有)。谢谢!
    • 为什么静态和动态部分的 MIME 类型相同?
    • 今天这些脚本调用救了我的命。我实际上已经在 appHost.config 中有该部分......但是当我应用脚本的每一行时,它说它找不到元素。所以这修改了其他东西,不知道在哪里,但它有效!
    • 添加这个为我解决了这个问题:
    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2013-05-16
    • 2011-04-05
    • 2012-06-03
    • 2015-01-23
    • 2018-09-17
    • 2011-01-29
    相关资源
    最近更新 更多