如何从ASP.NET MVC与IIS 7.5压缩Json结果

我很难让IIS 7正确地从ASP.NET MVC压缩Json结果。 我在IIS中启用了静态和dynamic压缩。 我可以用Fiddler来validation正常的文本/ html和类似的logging是压缩的。 查看请求,存在accept-encoding gzip头。 该响应具有mimetype“application / json”,但未被压缩。

我发现这个问题似乎与MimeType有关。 当我包含mimeType="*/*" ,我可以看到正确的gzip响应。 我怎样才能让IIS压缩而不使用通配符mimeType? 我认为这个问题与ASP.NET MVC生成内容types标题的方式有关。

CPU使用率远低于dynamic限制阈值。 当我从IIS检查跟踪日志时,可以看到由于未find匹配的MIMEtypes而无法压缩。

 <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" 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/x-javascript" enabled="true" /> <add mimeType="application/json" enabled="true" /> </dynamicTypes> <staticTypes> <add mimeType="text/*" enabled="true" /> <add mimeType="message/*" enabled="true" /> <add mimeType="application/x-javascript" enabled="true" /> <add mimeType="application/atom+xml" enabled="true" /> <add mimeType="application/xaml+xml" enabled="true" /> <add mimeType="application/json" enabled="true" /> </staticTypes> </httpCompression> 

确保您的%WinDir%\ System32 \ inetsrv \ config \ applicationHost.config包含这些:

 <system.webServer> <urlCompression doDynamicCompression="true" /> <httpCompression> <dynamicTypes> <add mimeType="application/json" enabled="true" /> <add mimeType="application/json; charset=utf-8" enabled="true" /> </dynamicTypes> </httpCompression> </system.webServer> 

从@AtanasKorchev的链接 。

正如@simon_weaver在评论中所说的,你可能会用64位Windows上的32位编辑器编辑错误的文件,使用notepad.exe来确保这个文件确实被修改了。

我已经成功地使用了这里强调的方法。

使用本指南

这些答案都没有为我工作。 我记下了application / json; charset = utf-8 mime-type。

我推荐这种方法
创buildCompressAttribute类,并设置目标动作。