Ale*_* Dn 3 iis-7 gzip web-config
我试图httpCompression
在IIS7 上配置.通过谷歌搜索,我发现它可以使用httpCompression
配置中的部分.问题是,我无法通过web.config使其工作.
当我applicationHost.config
根据需要在一切工作中进行配置时,我希望能够为每个应用程序而不是全局进行此配置.
我在改款的定义applicationHost.config
,以<section name="httpCompression" overrideModeDefault="Allow" />
和移动httpCompression
部分的web.config:
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<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="*/*" enabled="false" />
</staticTypes>
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
</httpCompression>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?看起来IIS根本没有从web.config读取压缩配置.
每次更改后,我都会使应用程序池回收,所以这不是问题.
根据此ServerFault答案:https://serverfault.com/a/125156/117212 - 您无法在web.config中更改httpCompression,它需要在applicationHost.config文件中完成.以下是我在Azure Web角色中使用的代码,用于修改applicationHost.config文件并添加用于压缩的mime类型:
using (var serverManager = new ServerManager())
{
var config = serverManager.GetApplicationHostConfiguration();
var httpCompressionSection = config.GetSection("system.webServer/httpCompression");
var dynamicTypesCollection = httpCompressionSection.GetCollection("dynamicTypes");
Action<string> fnCheckAndAddIfMissing = mimeType =>
{
if (dynamicTypesCollection.Any(x =>
{
var v = x.GetAttributeValue("mimeType");
if (v != null && v.ToString() == mimeType)
{
return true;
}
return false;
}) == false)
{
ConfigurationElement addElement = dynamicTypesCollection.CreateElement("add");
addElement["mimeType"] = mimeType;
addElement["enabled"] = true;
dynamicTypesCollection.AddAt(0, addElement);
}
};
fnCheckAndAddIfMissing("application/json");
fnCheckAndAddIfMissing("application/json; charset=utf-8");
serverManager.CommitChanges();
}
Run Code Online (Sandbox Code Playgroud)
ServerManager
来自Microsoft.Web.Administration
NuGet的包装.