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 在 IIS 8.0 中未修改專案 <httpCompression>
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. 在 [ 伺服器角色] 頁面上,依序展開 [Web 服務器] ([IIS) ]、[ 網頁伺服器]、[ 效能] ,然後選取 [ 靜態內容壓縮 ] 和/或 [ 動態內容壓縮]。 按一下 [下一步] 。
    Web 服務器和效能節點的螢幕擷取畫面,其中已選取 [靜態內容壓縮],並醒目提示 [動態內容壓縮]。 .
  5. 在 [選取功能] 頁面上,按 [下一步]
  6. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  7. 在 [結果] 頁面上,按一下 [關閉]

Windows 8 或Windows 8.1

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

Windows Server 2008 或 Windows Server 2008 R2

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

Windows Vista 或 Windows 7

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

作法

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

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

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

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

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

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

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

  3. 在 [ 首頁] 窗格中,按兩下 [壓縮]。
    預設網站首頁的螢幕擷取畫面,其中顯示已選取 [壓縮]。

  4. 在 [ 壓縮 ] 窗格中,核取啟用靜態或動態壓縮的方塊,或移除核取記號以停用靜態或動態壓縮。
    顯示 [壓縮] 窗格的螢幕擷取畫面,其中已選取 [啟用動態內容壓縮] 和 [啟用靜態內容壓縮] 方塊。

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

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

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

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

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows 8 或 Windows 8.1:

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

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

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager
  2. 在 [ 連線] 窗格中,反白顯示伺服器的名稱。

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

  4. 在 [ 壓縮 ] 窗格中,核取啟用靜態或動態壓縮的方塊,或移除核取記號以停用靜態或動態壓縮。
    [壓縮] 頁面的螢幕擷取畫面,其中顯示 [啟用動態內容壓縮] 和 [啟用靜態內容壓縮] 的這兩個方塊。

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

如何設定 staticCompressionIgnoreHitFrequency

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

    • 如果您使用 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
  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 選擇性的 Boolean 屬性。

指定所有壓縮檔案儲存在目錄屬性所指定的壓縮目錄中,是否具有磁碟空間限制。

預設值是 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 屬性。

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

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

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

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

IIS 7.5 的預設值為 2700 ;針對 IIS 7.0,預設值為 256
noCompressionForHttp10 選擇性的 Boolean 屬性。

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

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

預設值是 true
noCompressionForProxies 選擇性的 Boolean 屬性。

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

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

預設值是 true
noCompressionForRange 選擇性的 Boolean 屬性。

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

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

預設值是 true
sendCacheHeaders 選擇性的 Boolean 屬性。

指定快 取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 選擇性的 Boolean 屬性。

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

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

預設值是 False

子元素

元素 描述
scheme 選擇性項目。

指定壓縮配置 (Gzip 或 Deflate) IIS 用來壓縮用戶端要求。
dynamicTypes 選擇性項目。

指定動態壓縮的組態設定。
staticTypes 選擇性項目。

指定靜態壓縮的組態設定。

組態範例

下列預設 <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()