カスタム ヘッダー: <customHeaders>

概要

<httpProtocol> 要素の <customHeaders> 要素は、インターネット インフォメーション サービス (IIS) 7 が Web サーバーからの HTTP 応答で返すカスタム HTTP ヘッダーを指定します。

Note

HTTP ヘッダーは、Web サーバーからの応答で返される名前と値のペアです。 カスタム応答ヘッダーは、既定の HTTP ヘッダーと共にクライアントに送信されます。 リダイレクトが発生した場合にのみ応答で返されるリダイレクト応答ヘッダーとは異なり、カスタム応答ヘッダーはすべての応答で返されます。

互換性

バージョン メモ
IIS 10.0 <customHeaders> 要素は、IIS 10.0 では変更されませんでした。
IIS 8.5 <customHeaders> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 <customHeaders> 要素は、IIS 8.0 では変更されませんでした。
IIS 7.5 <customHeaders> 要素は、IIS 7.5 では変更されませんでした。
IIS 7.0 <httpProtocol> 要素の <customHeaders> 要素は IIS 7.0 で導入されました。
IIS 6.0 <customHeaders> 要素は、IIS 6.0 の HttpCustomHeaders メタベース オブジェクトを置き換えます。

段取り

<httpProtocol> 要素の <customHeaders> 要素は、IIS 7 の既定のインストールに含まれています。

操作方法

Web サイトまたはアプリケーションのカスタム HTTP ヘッダーを設定する方法

  1. インターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー][ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順にクリックします。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] をクリックします。
      • [管理ツール] をクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーの [スタート] をクリックし、[管理ツール] をポイントして、[インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順にクリックします。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ウィンドウで、カスタム HTTP ヘッダーを設定するサイト、アプリケーション、またはディレクトリに移動します。

  3. [ホーム] ウィンドウで、[HTTP 応答ヘッダー] をダブルクリックします。
    Screenshot that shows the Home pane with H T T P Response Headers selected.

  4. [HTTP 応答ヘッダー] ウィンドウで、[操作] ウィンドウの [追加] をクリックします。
    Screenshot of H T T P Response Headers pane with Add option displayed in the Actions pane.

  5. [カスタム HTTP 応答ヘッダーの追加] ダイアログ ボックスで、カスタム ヘッダーの名前と値を設定して、[OK] クリックします。
    Screenshot of Add Custom H T T P Header dialog box with fields for name and value for custom header.

構成

属性

なし。

子要素

要素 説明
add 省略可能な要素です。

<customHeaders> コレクションにカスタム応答ヘッダーを追加します。
clear 省略可能な要素です。

すべてのカスタム応答ヘッダーへの参照を <customHeaders> コレクションから削除します。
remove 省略可能な要素です。

1 つのカスタム応答ヘッダーへの参照を <customHeaders> コレクションから削除します。

構成サンプル

次の構成サンプルは、カスタム HTTP ヘッダーと値を設定します。

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

Note

次の既定の <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()