カスタム ヘッダー <customHeaders>

  • 概要
  • 互換性
  • セットアップ
  • 方法
  • 構成
  • サンプル コード

※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。

概要

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

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

互換性

  IIS 7.0 IIS 6.0
注意 <httpProtocol><customHeaders> は IIS 7.0 で新たに導入された要素です。 <customHeaders> 要素は、IIS 6.0 の HttpCustomHeaders メタベース オブジェクトに代わるものです。

セットアップ

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

方法

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

  1. タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。

  2. [接続] ウィンドウで、カスタム HTTP ヘッダーを設定するサイト、アプリケーション、またはディレクトリに移動します。

  3. [ホーム] ウィンドウで [HTTP 応答ヘッダー] をダブルクリックします。

    拡大

  4. [HTTP 応答ヘッダー] ページで、[操作] ウィンドウの [追加] をクリックします。

    拡大

  5. [カスタム HTTP 応答ヘッダーの追加] ダイアログ ボックスで、カスタム ヘッダーの名前と値を設定し、[OK] をクリックします。

    拡大

構成

属性

なし。

子要素

要素 説明
add オプションの要素。

<customHeaders> コレクションにカスタム応答ヘッダーを追加します。
clear オプションの要素。

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

構成サンプル

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

<configuration>
   <system.webServer>
      <httpProtocol>

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

: IIS 7.0 の ApplicationHost.config ファイルでは、次の既定の <httpProtocol> 要素が構成されています。

<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()