IIS 7.5 not compressing JSON when set in applicati

2019-04-09 09:14发布

问题:

I've been working on enabling JSON compression from one of our MVC3 sites. From various articles I've read it seems as though I should be able to set the application/json; charset=utf-8 MIME type in the applications web.config file. But doing so does not enable compression. But when added to the applicationhost.config file, it works. Am I missing something here?

My application web.config has the following added to it:

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <dynamicTypes>
        <add mimeType="application/javascript; charset=utf-8" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/json; charset=utf-8" enabled="true" />
    </dynamicTypes>
    <staticTypes>
        <add mimeType="application/javascript; charset=utf-8" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/json; charset=utf-8" enabled="true" />
    </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />

The server is running Windows Server 2008 R2 with IIS7.5. I've also installed & enabled Dynamic Compression for the site in question.

Any help would be greatly appreciated, as I do not wish to enable JSON compression for the entire server.

回答1:

HttpCompression section is defined AppHostOnly in ApplicationHost.config which prevents you from setting its properties in web.config.

The compression module reads only the server level properties from 'ApplicationHost.config' so even if you unlock the section (with appcmd or overrideModeDefault="Allow"), settings on lower level will be ignored.