共用方式為


自訂標頭 < customHeaders>

概觀

元素 <customHeaders><httpProtocol> 元素會指定 Internet Information Services (IIS) 7 傳回的自訂 HTTP 標頭,

注意

HTTP 標頭是 Web 服務器回應中所傳回的名稱和值組。 自訂回應標頭會連同預設 HTTP 標頭一起傳送至用戶端。 不同于重新導向回應標頭,只有在重新導向發生時,才會在回應中傳回自訂回應標頭。

相容性

版本 備註
IIS 10.0 <customHeaders> IIS 10.0 中未修改專案。
IIS 8.5 <customHeaders> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <customHeaders>
IIS 7.5 <customHeaders> 在 IIS 7.5 中修改專案。
IIS 7.0 元素 <customHeaders><httpProtocol> 元素是在 IIS 7.0 中引進。
IIS 6.0 元素 <customHeaders> 會取代 IIS 6.0 HttpCustomHeaders 中繼基底物件。

安裝程式

元素 <customHeaders><httpProtocol> 元素包含在 IIS 7 的預設安裝中。

作法

如何設定網站或應用程式的自訂 HTTP 標頭

  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) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

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

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

  3. 在 [ 首頁] 窗格中,按兩下 [HTTP 回應標頭]。
    顯示 [首頁] 窗格的螢幕擷取畫面,其中已選取 H T T P 回應標頭。

  4. 在 [HTTP 回應標頭]窗格中,按一下 [動作] 窗格中的 [新增...]。
    [H T T P 回應標頭] 窗格的螢幕擷取畫面,其中 [新增] 選項會顯示在 [動作] 窗格中。

  5. 在 [ 新增自訂 HTTP 回應標頭 ] 對話方塊中,設定自訂標頭的名稱和值,然後按一下 [ 確定]。
    [新增自訂 H T T P 標頭] 對話方塊的螢幕擷取畫面,其中包含自訂標頭的名稱和值欄位。

組態

屬性

無。

子元素

元素 描述
add 選擇性項目。

將自訂回應標頭新增至 <customHeaders> 集合。
clear 選擇性項目。

<customHeaders> 集合中移除自訂回應標頭的所有參考。
remove 選擇性項目。

從集合中移除自訂回應標頭的 <customHeaders> 參考。

組態範例

下列組態範例會設定自訂 HTTP 標頭和值。

<configuration>
   <system.webServer>
      <httpProtocol>
         <customHeaders>
            <add name="X-Custom-Name" value="MyCustomValue" />
         </customHeaders>
      </httpProtocol>
   </system.webServer>
</configuration>

注意

下列預設 <httpProtocol> 元素是在 IIS 7 的 ApplicationHost.config 檔案中設定。

<httpProtocol>
   <customHeaders>
      <clear />
      <add name="X-Powered-By" value="ASP.NET" />
   </customHeaders>
   <redirectHeaders>
      <clear />
   </redirectHeaders>
</httpProtocol>

範例程式碼

下列程式碼範例會設定自訂 HTTP 標頭和值。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpProtocol /+"customHeaders.[name='X-Custom-Name',value='MyCustomValue']"

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.GetWebConfiguration("Default Web Site");
         ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
         ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");

         ConfigurationElement addElement = customHeadersCollection.CreateElement("add");
         addElement["name"] = @"X-Custom-Name";
         addElement["value"] = @"MyCustomValue";
         customHeadersCollection.Add(addElement);

         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.GetWebConfiguration("Default Web Site")
      Dim httpProtocolSection As ConfigurationSection = config.GetSection("system.webServer/httpProtocol")
      Dim customHeadersCollection As ConfigurationElementCollection = httpProtocolSection.GetCollection("customHeaders")

      Dim addElement As ConfigurationElement = customHeadersCollection.CreateElement("add")
      addElement("name") = "X-Custom-Name"
      addElement("value") = "MyCustomValue"
      customHeadersCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";
var httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var customHeadersCollection = httpProtocolSection.ChildElements.Item("customHeaders").Collection;

var addElement = customHeadersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "X-Custom-Name";
addElement.Properties.Item("value").Value = "MyCustomValue";
customHeadersCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set customHeadersCollection = httpProtocolSection.ChildElements.Item("customHeaders").Collection

Set addElement = customHeadersCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "X-Custom-Name"
addElement.Properties.Item("value").Value = "MyCustomValue"
customHeadersCollection.AddElement(addElement)

adminManager.CommitChanges()