简体   繁体   English

IIS 7中的JSON Web服务响应压缩

[英]JSON webservice response compression in IIS 7

I have trouble with JSON response compression. 我在JSON响应压缩方面遇到麻烦。 I look to response headers after uploading website to production server (Windows 2008, IIS 7) and found uncompressed response. 将网站上传到生产服务器(Windows 2008,IIS 7)后,我查看响应标头,发现未压缩的响应。 Turning on "Enabled static compression" and ""Enable dynamic compression" in IIS control panel does not effect. ASPX pages was responsed gzipped, but webservice response uncompressed. 在IIS控制面板中启用“启用静态压缩”和“启用动态压缩”无效。ASPX页面已被压缩,但是webservice响应未压缩。

I looked to google, but no answer found about this trouble. 我看了看Google,但没有找到关于此问题的答案。 Also, I try this json ihttpmodule compression way (and adding to web.config this module) - but this source is excellent working at development machine with ASP.NET development server (and have seven times response size reduced) and totally ignored at IIS7. 另外,我尝试了这种json ihttpmodule压缩方式(并将此模块添加到web.config中)-但是该源代码非常适合在具有ASP.NET开发服务器的开发机上工作(并且响应大小减小了7倍),而在IIS7上完全被忽略了。

How I can apply gzip compression to json responses from my webservice? 如何将gzip压缩应用于来自Web服务的json响应? Thanks. 谢谢。

PS .NET 3.5 PS .NET 3.5

I stumbled across the same problem with JsonCompressionModule. 我偶然发现了与JsonCompressionModule相同的问题。 It was working on the development server but not on IIS 7. I figured out that under IIS 7 it isn't enough to add the handle under system.web but also under system.webServer (see below). 它只能在开发服务器上运行,而不能在IIS 7上运行。我发现,在IIS 7下,仅在system.web和system.webServer下添加句柄是不够的(请参见下文)。 After this change it works fine on IIS 7. 进行此更改后,它可以在IIS 7上正常工作。

<system.web>
 <httpModules>
   <add name="JsonCompressionModule" type="JsonCompressionModule"/>
 </httpModules>
</system.web>

<system.webServer>
 <modules>
  <add name="JsonCompressionModule" preCondition="managedHandler" type="JsonCompressionModule"/>
 </modules>
</system.webServer>

IIS dynamic compression uses the response content type header to determine if it should compress the content or not. IIS动态压缩使用响应内容类型标头来确定是否应压缩内容。 The default settings do not compress "application/json". 默认设置不压缩“ application / json”。 You can find more information at http://www.iis.net/configreference/system.webserver/httpcompression 您可以在http://www.iis.net/configreference/system.webserver/httpcompression中找到更多信息

To add it, open an admin command prompt, run the commands below then restart the IIS service. 要添加它,请打开一个管理命令提示符,运行以下命令,然后重新启动IIS服务。

cd \Windows\System32\inetsrv
appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost

You may want to add a few other content types. 您可能需要添加其他一些内容类型。 The only application/* types the default dynamic settings compress is application/x-javascript. 默认动态设置压缩的唯一application / *类型是application / x-javascript。

Please try and examine what are the request headers which the client sends. 请尝试检查客户端发送的请求标头是什么。 Accept-Encoding should have gzip or deflate value. Accept-Encoding应该具有gzip或deflate值。 Make sure though that the client is able to decompress JSON. 确保客户端能够解压缩JSON。 There is a solution which is able to set Accept-Encoding and perform GZIP compression all together—Helicon Ape ( http://www.helicontech.com/ape/ ). Helicon Ape( http://www.helicontech.com/ape/ )是一种可以一起设置Accept-Encoding和执行GZIP压缩的解决方案。 The following configuration will do both the tricks: 以下配置将完成这两个技巧:

# Manually set required request header
RequestHeader append Accept-Encoding gzip early

# Enable high-level (9) comression for JSON files
SetEnvIf mime application/json gzip=9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM