共用方式為


HTTP 壓縮 <HTTPCompression>

概觀

元素 <httpCompression> 會指定 Internet Information Services (IIS) 7 的 HTTP 壓縮設定。 HTTP 壓縮可以在 IIS 和用戶端瀏覽器之間提供更快速的傳輸時間,以接受壓縮檔。

備註

HTTP 用戶端必須藉由傳送適當的 HTTP Accept-encoding 標頭來起始壓縮內容的通訊。 如果客戶端無法進行 HTTP 壓縮,則不會傳遞該標頭,且 IIS 7 一律會傳回未壓縮的內容。

IIS 7 使用的壓縮類型有兩種:

  • 靜態壓縮

    IIS 7 會在 目錄 屬性所指定的路徑中快取壓縮的靜態內容,藉由不需要重新壓縮已經壓縮的內容來提升壓縮效能。 在 IIS 7 壓縮檔案之後,後續要求會從快取目錄提供檔案的壓縮複本。

    staticCompressionEnableCpuUsagestaticCompressionDisableCpuUsage 屬性會指定 IIS 7 何時會根據 CPU 使用量壓縮靜態檔案。

    您應該使用靜態壓縮搭配通常不會變更的檔案,例如 HTML 檔案(*.html、*.htm)、文本檔(*.txt)、Microsoft Office 檔(*.doc、*.xls、*.ppt等。這些檔案的大小可以透過壓縮來減少,這樣可減少用戶端要求的下載時間,並減少伺服器上的頻寬。

    備註

    *.jpg 和 *.png 檔案等影像檔也是靜態檔案,但通常不會受益於 HTTP 壓縮,因為這些圖像文件已經壓縮。

  • 動態壓縮

    不同於靜態壓縮,IIS 7 會在每次用戶端要求內容時執行動態壓縮,但壓縮的版本不會快取到磁碟。 由於靜態和動態內容之間的主要差異,因此會進行這項變更。 靜態內容不會變更。 不過,動態內容通常是應用程式所建立的內容,因此通常會變更,例如 Active Server Pages (ASP) 或 ASP.NET 內容。 因為動態內容應該經常變更,所以 IIS 7 不會快取它。

    dynamicCompressionEnableCpuUsagedynamicCompressionDisableCpuUsage 屬性會指定 IIS 7 何時會根據 CPU 使用量壓縮動態檔案。

IIS 7 支援兩種不同的業界標準壓縮配置:

這兩個壓縮配置都可以使用 <schemes> 元素的 <httpCompression> 元素來啟用。

備註

雖然 元素 <httpCompression> 指定 Internet Information Services (IIS) 7 的 HTTP 壓縮設定, <但 urlCompression> 元素會指定是否啟用 URL 命名空間的壓縮。

相容性

版本 備註
IIS 10.0 <httpCompression> 在 IIS 10.0 中修改專案。
IIS 8.5 屬性 staticCompressionIgnoreHitFrequency 已在 IIS 8.5 中新增。
IIS 8.0 專案 <httpCompression> 未在 IIS 8.0 中修改。
IIS 7.5 在 IIS 7.5 中,屬性的 minFileSizeForComp 預設值已變更,而且 dynamicCompressionBufferLimit 已新增屬性。
IIS 7.0 元素 <httpCompression> 是在 IIS 7.0 中引進的。
IIS 6.0 元素 <httpCompression> 會取代下列 IIS 6.0 中繼基底屬性:
  • HcCacheControlHeader
  • HcCompressionDirectory
  • HcDoDiskSpaceLimiting
  • HcExpiresHeader
  • HcMaxDiskSpaceUsage
  • HcMinFileSizeForComp
  • HcNoCompressionForHttp10
  • HcNoCompressionForProxies
  • HcNoCompressionForRange
  • HcSendCacheHeaders

設定

HTTP 壓縮通常會在 IIS 7 和更新版本的預設安裝上使用。 不過,預設只會安裝靜態壓縮。 若要安裝靜態或動態壓縮,請使用下列步驟。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任務欄上,按兩下 [伺服器管理員]。
  2. [伺服器管理員] 中,按兩下 [ 管理] 功能表,然後按下 [ 新增角色和功能]。
  3. 在 [ 新增角色和功能 精靈] 中,按 [下一步]。 選取安裝類型,然後按 [ 下一步]。 選取目的地伺服器,然後按 [下一步]。
  4. 在 [ 伺服器角色] 頁面上,依序展開 [網頁伺服器]、[ 網頁伺服器] 和 [ 效能],然後選取 [靜態內容壓縮 ] 和/或 [ 動態內容壓縮]。 按一下 [下一步]
    已選取 [靜態內容壓縮] 和 [動態內容壓縮] 的網頁伺服器和效能節點螢幕快照,並醒目提示 [動態內容壓縮]。 .
  5. 在 [選取功能] 頁面上,點擊 下一步
  6. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  7. 在 [ 結果] 頁面上,按兩下 [ 關閉]。

Windows 8 或 Windows 8.1

  1. 在 [ 開始] 畫面上,將指標一路移至左下角,以滑鼠右鍵按兩下 [ 開始 ] 按鈕,然後按兩下 [ 控制面板]。
  2. [控制面板] 中,按兩下 [ 程式和功能],然後按兩下 [ 開啟或關閉 Windows 功能]。
  3. 序展開 [Internet Information Services]、[ 萬維網服務]、[ 效能功能],然後選取 [動態內容壓縮 ] 和/或 [ 靜態內容壓縮]。
    [萬維網與效能功能] 窗格展開的螢幕快照,其中已選取動態內容壓縮和靜態內容壓縮。
  4. 按一下 [確定]。
  5. 按下 關閉

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任務欄上,按兩下 [ 開始],指向 [系統管理工具],然後按兩下 [ 伺服器管理員]。
  2. 在 [伺服器管理員階層] 窗格中,展開 [角色],然後按兩下 [網頁伺服器] [IIS]。
  3. [Web 伺服器 (IIS)] 窗格中,捲動至 [ 角色服務 ] 區段,然後按兩下 [ 新增角色服務]。
  4. 在 [新增角色服務精靈] 的 [選取角色服務] 頁面上,如果您想要安裝動態壓縮和靜態內容壓縮,如果您想要安裝靜態壓縮,請選取 [動態內容壓縮],然後按 [下一步]。
    [新增角色服務] 頁面中展開 [效能] 節點的螢幕快照,其中已醒目提示動態內容壓縮。
  5. 在 [ 確認安裝選取專案] 頁面上,按兩下 [ 安裝]。
  6. 在 [ 結果] 頁面上,按兩下 [ 關閉]。

Windows Vista 或 Windows 7

  1. 在任務欄上,按兩下 [ 開始],然後按兩下 [ 控制面板]。
  2. [控制面板] 中,按兩下 [ 程式和功能],然後按兩下 [ 開啟或關閉 Windows 功能]。
  3. 序展開 [Internet Information Services]、[ 萬維網服務] 和 [ 效能功能]。
  4. 如果您想要安裝動態壓縮,請選取 [Http Compression Dynamic ],如果您想要安裝靜態壓縮,請選取 [ 靜態內容壓縮 ]。
    已展開 [效能功能] 節點的螢幕快照,並已選取 [H T T P 壓縮動態]。
  5. 按一下 [確定]。

如何做

如何啟用或停用網站或應用程式的靜態和動態壓縮

  1. 開啟 Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任務欄上,按兩下 [ 伺服器管理員]、[ 工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住 Windows 鍵,按字母 X,然後按兩下 [ 控制面板]。
      • 按兩下 [系統管理工具],然後按兩下 [Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任務欄上,按兩下 [ 開始],指向 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在任務欄上,按兩下 [ 開始],然後按兩下 [ 控制面板]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
  2. 在 [ 連線] 窗格中,移至您要啟用壓縮的連線、月臺、應用程式或目錄。

  3. 在 [ 首頁] 窗格中,按兩下 [ 壓縮]。
    默認網站首頁的螢幕快照,其中顯示已選取 [壓縮]。

  4. 在 [ 壓縮 ] 窗格中,核取啟用靜態或動態壓縮的方塊,或移除停用靜態或動態壓縮的複選標記。
    顯示 [壓縮] 窗格的螢幕快照,其中包含 [啟用動態內容壓縮] 和 [啟用靜態內容壓縮] 方塊。

  5. 完成上述步驟之後,請按兩下 [動作] 窗格中的 [套用]。

如何啟用或停用伺服器的靜態和動態壓縮

  1. 開啟 Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任務欄上,按兩下 [ 伺服器管理員]、[ 工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住 Windows 鍵,按字母 X,然後按兩下 [ 控制面板]。
      • 按兩下 [系統管理工具],然後按兩下 [Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任務欄上,按兩下 [ 開始],指向 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在任務欄上,按兩下 [ 開始],然後按兩下 [ 控制面板]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
  2. 在 [ 連線] 窗格中,反白顯示伺服器的名稱。

  3. 在伺服器的 [ 首頁 ] 窗格中,按兩下 [ 壓縮]。
    顯示 [伺服器首頁] 窗格的螢幕快照,其中已醒目提示 [壓縮]。

  4. 在 [ 壓縮 ] 窗格中,核取啟用靜態或動態壓縮的方塊,或移除停用靜態或動態壓縮的複選標記。
    [壓縮] 頁面的螢幕快照,其中顯示 [啟用動態內容壓縮] 和 [啟用靜態內容壓縮] 的兩個方塊。

  5. 完成上述步驟之後,請按兩下 [動作] 窗格中的 [套用]。

如何設定 staticCompressionIgnoreHitFrequency

  1. 開啟 Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 R2:

      • 在任務欄上,按兩下 [ 伺服器管理員]、[ 工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows 8.1:

      • 按住 Windows 鍵,按字母 X,然後按兩下 [ 控制面板]。
      • 按兩下 [系統管理工具],然後按兩下 [Internet Information Services (IIS) 管理員]。
  2. 在 [ 連線] 窗格中,選取伺服器,然後按兩下 [ 組態編輯器]。

  3. 在 [ 組態編輯器] 的 區段中,選取 system.webServer,然後選取 [HTTPCompression]。

  4. 針對 staticCompressionIgnoreHitFrequency,輸入 True 以停用靜態檔案只有在一段時間內達到特定次數,或輸入 False 以啟用行為時,才會壓縮靜態檔案的行為。
    此螢幕快照顯示 [組態編輯器] 頁面,針對 [靜態壓縮忽略點擊頻率] 選項輸入 True。

  5. 在 [ 動作] 窗格中,按兩下 [ 套用]

設定

屬性

這些屬性只能在伺服器層級設定。 將會忽略月臺層級或更低層級的設定。

屬性 說明
cacheControlHeader 選擇性字串屬性。

指定 IIS 新增至覆寫 HTTP Expires 標頭之 Cache-Control 標頭的 指示詞。 此屬性可確保較舊的用戶端和 Proxy 伺服器不會嘗試快取壓縮檔案。 若要啟用此設定,您必須將 sendCacheHeaders 屬性設定為 true。 WWW 服務必須先重新啟動,才能生效此屬性的變更。

預設值是 max-age=86400
directory 選擇性字串屬性。

指定暫時儲存和快取靜態檔案壓縮版本的目錄。

注意: 在 IIS 6.0 中,IIS 暫存壓縮檔的預設目錄路徑 %windir%\IIS 暫存壓縮檔案。

預設值是 %SystemDrive%\inetpub\temp\IIS Temporary Compressed Files
doDiskSpaceLimiting 選擇性布爾值屬性。

指定儲存在目錄屬性所指定壓縮目錄中的所有壓縮檔空間量是否有限制。

預設值是 true
dynamicCompressionBufferLimit 選擇性 uint 屬性。

指定 IIS 在將緩衝區排清至用戶端之前,要緩衝的動態壓縮數據量上限。 這會減少執行動態壓縮所需的記憶體數量。

注意: 這個屬性已在 IIS 7.5 中新增。

預設值是 65536
dynamicCompressionDisableCpuUsage 選擇性 uint 屬性。

指定將停用動態壓縮的CPU使用率百分比。

注意: 這個屬性會作為關閉動態壓縮的CPU上限。 當 CPU 使用率低於 dynamicCompressionEnableCpuUsage 屬性中指定的值時,將會重新啟用動態壓縮。

預設值是 90
dynamicCompressionEnableCpuUsage 選擇性 uint 屬性。

指定將啟用動態壓縮的CPU使用率百分比。 值必須介於 0 到 100 之間。 平均 CPU 使用率每 30 秒計算一次。

注意: 此屬性的作用是低於開啟動態壓縮的CPU限制。 當 CPU 使用率高於 dynamicCompressionDisableCpuUsage 屬性中指定的值時,將會停用動態壓縮。

預設值是 50
expiresHeader 選擇性字串屬性。

指定 HTTP Expires 標頭的內容,該標頭會與所有要求的壓縮檔一起傳送,以及 cacheControlHeader 屬性中指定的 Cache-Control 標頭。 這種標頭組合可確保較舊的用戶端和 Proxy 伺服器不會嘗試快取壓縮檔案。 若要啟用此設定,您必須將 sendCacheHeaders 屬性設定為 true。 萬維網發佈服務 (WWW 服務) 必須先重新啟動,此屬性的變更才會生效。

預設值是 Wed, 01 Jan 1997 12:00:00 GMT
maxDiskSpaceUsage 選擇性 uint 屬性。

指定壓縮檔案可以在壓縮目錄中佔用的 MB 磁碟空間數目。 當壓縮檔案所使用的空間超過此屬性中的 90% 值時,IIS 會刪除最近使用最少的檔案,直到達到 90% 的使用層級為止。

注意: 在 IIS 6.0 中,此限制是以位元組表示;在 IIS 7 中,它會以 MB 表示。 在 IIS 7 中,每個應用程式集區會套用限制。

預設值是 100
minFileSizeForComp 選擇性 uint 屬性。

指定檔案必須包含的最小位元節數目,才能使用隨選壓縮。

IIS 7.5 的預設值為 2700;對於 IIS 7.0,預設值為 256
noCompressionForHttp10 選擇性布爾值屬性。

指定是否針對包含 HTTP 1.0 版本號碼的要求停用壓縮。

注意: 某些 HTTP 1.0 用戶端無法正確處理壓縮物件的快取。 您可以使用此設定來避免將壓縮檔傳回無法解壓縮的用戶端。

預設值是 true
noCompressionForProxies 選擇性布爾值屬性。

指定是否針對透過 Proxy 伺服器提出的壓縮要求停用 HTTP 1.1 回應。

注意: 某些 HTTP Proxy 伺服器不會正確處理壓縮物件的快取。 您可以使用此設定來避免將壓縮檔傳回至無法解壓縮的 Proxy 伺服器。

預設值是 true
noCompressionForRange 選擇性布爾值屬性。

指定是否針對包含 Range 標頭的 HTTP 要求停用壓縮。

注意: 某些客戶端無法正確處理範圍要求。 您可以使用此設定來避免將壓縮檔傳回無法解壓縮的用戶端。

預設值是 true
sendCacheHeaders 選擇性布爾值屬性。

指定 快取ControlHeaderexpiresHeader 中設定的標頭是否隨著每個壓縮的回應一起傳送。

預設值是 false
staticCompressionDisableCpuUsage 選擇性 uint 屬性。

指定停用靜態壓縮的CPU使用率百分比。 值必須介於 0 到 100 之間。 平均 CPU 使用率每 30 秒計算一次。

注意: 這個屬性會作為關閉靜態壓縮的CPU上限。 當 CPU 使用率低於 staticCompressionEnableCpuUsage 屬性中指定的值時,將會重新啟用靜態壓縮。

預設值是 100
staticCompressionEnableCpuUsage 選擇性 uint 屬性。

指定啟用靜態壓縮的CPU使用率百分比。 值必須介於 0 到 100 之間。 平均 CPU 使用率每 30 秒計算一次。

注意: 這個屬性的作用是低於開啟靜態壓縮的CPU限制。 當 CPU 使用率高於 staticCompressionDisableCpuUsage 屬性中指定的值時,將會停用靜態壓縮。

預設值是 50
staticCompressionIgnoreHitFrequency 選擇性布爾值屬性。

如果 為 True,則只有在一段時間內達到特定次數時,才會停用靜態檔案壓縮的行為。 您可能會遇到一律要壓縮靜態內容以降低頻寬使用量的情況。 例如,當系統在 Web 伺服器與要求者之間採用邊緣快取的負載平衡器時,您可能會想要一律壓縮靜態內容,而導致未壓縮的檔案在邊緣伺服器上快取,因為後續的要求不會到達網頁伺服器。

如果未停用此行為,10 秒內大於或等於兩次點擊的命中率會導致靜態內容壓縮。 較低的命中率會導致內容未壓縮。

預設值是 False

子專案

元素 說明
scheme 選擇性專案。

指定壓縮配置 (Gzip 或 Deflate) IIS 用來壓縮用戶端要求。 這個專案只能在伺服器層級設定。
dynamicTypes 選擇性專案。

指定動態壓縮的組態設定。 針對 IIS 10.0 之前的 IIS 版本,只能在伺服器層級設定。 針對 IIS 10.0 和更新版本,也可以在月臺層級設定。
staticTypes 選擇性專案。

指定靜態壓縮的組態設定。 針對 IIS 10.0 和更新版本,只能在月臺層級設定。 針對 IIS 10.0 之前的 IIS 版本,只能在伺服器層級設定。 針對 IIS 10.0 和更新版本,也可以在月臺層級設定。

組態範例

下列預設 <httpCompression> 專案是在 IIS 7 ApplicationHost.config 檔案中設定。 除非您使用 元素,否則此組態區段會繼承預設組 <clear> 態設定。

<httpCompression
      directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
   <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
   <dynamicTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
   </dynamicTypes>
   <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
   </staticTypes>
</httpCompression>

範例程式碼

下列程式代碼範例會將 Office 2003 檔的 MIME 類型新增至動態壓縮類型清單。

備註

Office 2007 檔使用內建壓縮,因此不需要由 IIS 壓縮。

AppCmd.exe

appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/msword',enabled='True']" /commit:apphost

appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/vnd.ms-powerpoint',enabled='True']" /commit:apphost

appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/vnd.ms-excel',enabled='True']" /commit:apphost

備註

當您使用 AppCmd.exe 來設定這些設定時,請務必將 認可 參數 apphost 設定為 。 這會將組態設定認可至 ApplicationHost.config 檔案中的適當位置區段。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection httpCompressionSection = config.GetSection("system.webServer/httpCompression");
         ConfigurationElementCollection dynamicTypesCollection = httpCompressionSection.GetCollection("dynamicTypes");

         ConfigurationElement addElement = dynamicTypesCollection.CreateElement("add");
         addElement["mimeType"] = @"application/msword";
         addElement["enabled"] = true;
         dynamicTypesCollection.Add(addElement);

         ConfigurationElement addElement1 = dynamicTypesCollection.CreateElement("add");
         addElement1["mimeType"] = @"application/vnd.ms-powerpoint";
         addElement1["enabled"] = true;
         dynamicTypesCollection.Add(addElement1);

         ConfigurationElement addElement2 = dynamicTypesCollection.CreateElement("add");
         addElement2["mimeType"] = @"application/vnd.ms-excel";
         addElement2["enabled"] = true;
         dynamicTypesCollection.Add(addElement2);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim httpCompressionSection As ConfigurationSection = config.GetSection("system.webServer/httpCompression")
      Dim dynamicTypesCollection As ConfigurationElementCollection = httpCompressionSection.GetCollection("dynamicTypes")

      Dim addElement As ConfigurationElement = dynamicTypesCollection.CreateElement("add")
      addElement("mimeType") = "application/msword"
      addElement("enabled") = True
      dynamicTypesCollection.Add(addElement)

      Dim addElement1 As ConfigurationElement = dynamicTypesCollection.CreateElement("add")
      addElement1("mimeType") = "application/vnd.ms-powerpoint"
      addElement1("enabled") = True
      dynamicTypesCollection.Add(addElement1)

      Dim addElement2 As ConfigurationElement = dynamicTypesCollection.CreateElement("add")
      addElement2("mimeType") = "application/vnd.ms-excel"
      addElement2("enabled") = True
      dynamicTypesCollection.Add(addElement2)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var httpCompressionSection = adminManager.GetAdminSection("system.webServer/httpCompression", "MACHINE/WEBROOT/APPHOST");
var dynamicTypesCollection = httpCompressionSection.ChildElements.Item("dynamicTypes").Collection;

var addElement = dynamicTypesCollection.CreateNewElement("add");
addElement.Properties.Item("mimeType").Value = "application/msword";
addElement.Properties.Item("enabled").Value = true;
dynamicTypesCollection.AddElement(addElement);

var addElement1 = dynamicTypesCollection.CreateNewElement("add");
addElement1.Properties.Item("mimeType").Value = "application/vnd.ms-powerpoint";
addElement1.Properties.Item("enabled").Value = true;
dynamicTypesCollection.AddElement(addElement1);

var addElement2 = dynamicTypesCollection.CreateNewElement("add");
addElement2.Properties.Item("mimeType").Value = "application/vnd.ms-excel";
addElement2.Properties.Item("enabled").Value = true;
dynamicTypesCollection.AddElement(addElement2);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set httpCompressionSection = adminManager.GetAdminSection("system.webServer/httpCompression", "MACHINE/WEBROOT/APPHOST")
Set dynamicTypesCollection = httpCompressionSection.ChildElements.Item("dynamicTypes").Collection

Set addElement = dynamicTypesCollection.CreateNewElement("add")
addElement.Properties.Item("mimeType").Value = "application/msword"
addElement.Properties.Item("enabled").Value = True
dynamicTypesCollection.AddElement(addElement)

Set addElement1 = dynamicTypesCollection.CreateNewElement("add")
addElement1.Properties.Item("mimeType").Value = "application/vnd.ms-powerpoint"
addElement1.Properties.Item("enabled").Value = True
dynamicTypesCollection.AddElement(addElement1)

Set addElement2 = dynamicTypesCollection.CreateNewElement("add")
addElement2.Properties.Item("mimeType").Value = "application/vnd.ms-excel"
addElement2.Properties.Item("enabled").Value = True
dynamicTypesCollection.AddElement(addElement2)

adminManager.CommitChanges()

下列程式代碼範例會將 Office 2003 檔的 MIME 類型新增至靜態壓縮類型清單。

( [> !注意]

Office 2007 檔使用內建壓縮,因此不需要由 IIS 壓縮。

AppCmd.exe

appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/msword',enabled='True']" /commit:apphost

appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/vnd.ms-powerpoint',enabled='True']" /commit:apphost

appcmd.exe set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/vnd.ms-excel',enabled='True']" /commit:apphost

備註

當您使用 AppCmd.exe 來設定這些設定時,請務必將 認可 參數 apphost 設定為 。 這會將組態設定認可至 ApplicationHost.config 檔案中的適當位置區段。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection httpCompressionSection = config.GetSection("system.webServer/httpCompression");
         ConfigurationElementCollection staticTypesCollection = httpCompressionSection.GetCollection("staticTypes");

         ConfigurationElement addElement = staticTypesCollection.CreateElement("add");
         addElement["mimeType"] = @"application/msword";
         addElement["enabled"] = true;
         staticTypesCollection.Add(addElement);

         ConfigurationElement addElement1 = staticTypesCollection.CreateElement("add");
         addElement1["mimeType"] = @"application/vnd.ms-powerpoint";
         addElement1["enabled"] = true;
         staticTypesCollection.Add(addElement1);

         ConfigurationElement addElement2 = staticTypesCollection.CreateElement("add");
         addElement2["mimeType"] = @"application/vnd.ms-excel";
         addElement2["enabled"] = true;
         staticTypesCollection.Add(addElement2);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim httpCompressionSection As ConfigurationSection = config.GetSection("system.webServer/httpCompression")
      Dim staticTypesCollection As ConfigurationElementCollection = httpCompressionSection.GetCollection("staticTypes")

      Dim addElement As ConfigurationElement = staticTypesCollection.CreateElement("add")
      addElement("mimeType") = "application/msword"
      addElement("enabled") = True
      staticTypesCollection.Add(addElement)

      Dim addElement1 As ConfigurationElement = staticTypesCollection.CreateElement("add")
      addElement1("mimeType") = "application/vnd.ms-powerpoint"
      addElement1("enabled") = True
      staticTypesCollection.Add(addElement1)

      Dim addElement2 As ConfigurationElement = staticTypesCollection.CreateElement("add")
      addElement2("mimeType") = "application/vnd.ms-excel"
      addElement2("enabled") = True
      staticTypesCollection.Add(addElement2)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var httpCompressionSection = adminManager.GetAdminSection("system.webServer/httpCompression", "MACHINE/WEBROOT/APPHOST");
var staticTypesCollection = httpCompressionSection.ChildElements.Item("staticTypes").Collection;

var addElement = staticTypesCollection.CreateNewElement("add");
addElement.Properties.Item("mimeType").Value = "application/msword";
addElement.Properties.Item("enabled").Value = true;
staticTypesCollection.AddElement(addElement);

var addElement1 = staticTypesCollection.CreateNewElement("add");
addElement1.Properties.Item("mimeType").Value = "application/vnd.ms-powerpoint";
addElement1.Properties.Item("enabled").Value = true;
staticTypesCollection.AddElement(addElement1);

var addElement2 = staticTypesCollection.CreateNewElement("add");
addElement2.Properties.Item("mimeType").Value = "application/vnd.ms-excel";
addElement2.Properties.Item("enabled").Value = true;
staticTypesCollection.AddElement(addElement2);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set httpCompressionSection = adminManager.GetAdminSection("system.webServer/httpCompression", "MACHINE/WEBROOT/APPHOST")
Set staticTypesCollection = httpCompressionSection.ChildElements.Item("staticTypes").Collection

Set addElement = staticTypesCollection.CreateNewElement("add")
addElement.Properties.Item("mimeType").Value = "application/msword"
addElement.Properties.Item("enabled").Value = True
staticTypesCollection.AddElement(addElement)

Set addElement1 = staticTypesCollection.CreateNewElement("add")
addElement1.Properties.Item("mimeType").Value = "application/vnd.ms-powerpoint"
addElement1.Properties.Item("enabled").Value = True
staticTypesCollection.AddElement(addElement1)

Set addElement2 = staticTypesCollection.CreateNewElement("add")
addElement2.Properties.Item("mimeType").Value = "application/vnd.ms-excel"
addElement2.Properties.Item("enabled").Value = True
staticTypesCollection.AddElement(addElement2)

adminManager.CommitChanges()