2013-03-18 97 views
3

我試圖在IIS7配置httpCompression。通過谷歌搜索,我發現它可以在配置中使用httpCompression部分。這個問題,我不能讓它從web.config工作。IIS7 gzip壓縮 - httpCompression部分

當我做出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> 

我缺少什麼?它看起來像IIS不從web.config讀取壓縮配置。

每次更改之後,我讓應用程序池回收,所以它不是一個問題。

回答

3

您應檢查整個config file hierarchy

如果您從applicationHost中刪除該部分,則可能從machine.configweb.config繼承父目錄。

+2

我找到了解決辦法。我修改區段定義中的applicationHost.config從<段名稱= 「httpCompression」 overrideModeDefault = 「允許」/>,以<段名= 「httpCompression」 allowDefinition的= 「無處不在」 overrideModeDefault = 「允許」/>(allowDefinition的= 「無處不在」解決了這個問題)。謝謝 – 2013-03-18 11:54:51

+0

@AlexDn:我想你應該把你的解決方案作爲實際的答案。 – sharptooth 2013-11-07 14:44:20

+0

@sharptooth其實我只能在IIS服務器級別配置它。沒有找到每個應用程序配置的解決方案。 – 2013-11-07 17:06:36

6

按照此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(); 
} 

ServerManager來自Microsoft.Web.Administration包的NuGet。