A few years ago I blogged about enabling gzip compression for ASP.NET MVC applications; https://bartwullems.blogspot.com/2013/03/enable-dynamic-compression-for-aspnet.html. I ended my blogpost with the following remark:
‘Maybe it should work too if you add this to your web.config, but I couldn’t get this working’
This week I had to configure dynamic compression again, so I decided to search for a solution(again).
When scrolling through the applicationHost.config file I noticed the following section:
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
Notice that there are 2 attributes defined on the httpCompression section; overrideModeDefault and allowDefinition.
From the documentation:
The overrideModeDefault attribute is an optional attribute that defines the locked state of a section. Its available values are either Allow or Deny. The default value is "Allow". All IIS sections that are related to any performance, security or critical aspect of the server are locked with this attribute set to "Deny". If the overrideModeDefault attribute is set to "Deny", then any configuration files at a lower level (i.e. web.config files) that set a value for a property for the specific configuration section are not able to take effect and override the global values. This incurs in a lock violation and an error occurs.
The allowDefinition attribute is another optional attribute that defines the level of the hierarchy in which the section can be defined and properties can be set. If its value is MachineOnly, then the section can be set only in applicationHost.config or machine.config. If its value is MachineToRootWeb, then the section can be set either in the MachineOnly files or also in the root web.config. If its value is MachineToApplication, then the section can be either set in all the previous three files or also in web.config files in the application root folder. And lastly, if its value is Everywhere (which is the default) it can be set in any configuration file, whether the ones that affect configuration globally or in web.config files that apply to a given site, application or virtual directory.
This explains why setting this configuration in the web.config file didn’t work. This specific section is locked at IIS level.
If you still want to override it in your web.config, you can alter this section to:
<section name="httpCompression" allowDefinition="Everywhere" overrideModeDefault="Allow" />