ハンドラー: <handlers>

概要

<handlers> 要素では、特定のファイル名拡張子または URL に対して登録されるハンドラーを定義します。

ハンドラーはインターネット インフォメーション サービス (IIS) のコンポーネントであり、通常は要求リソースに対する応答を生成するため、特定のコンテンツへの要求を処理するように構成されます。 たとえば、ASP.NET Web ページはハンドラーの 1 つの種類です。 ハンドラーを使うと、静的ファイルではない情報をユーザーに返す必要があるリソースへの要求を処理できます。 IIS には、静的ファイル、Common Gateway Interface (CGI) アプリケーション、FastCGI アプリケーション、Internet Server API (ISAPI) 拡張機能をサポートするため、複数の異なるハンドラーの種類が用意されています。 ASP.NET では、HTTP ハンドラー インターフェイスを通じて追加のハンドラーの種類のサポートも提供されます。

開発者は特別な処理を実行するカスタム ハンドラーを作成でき、それはアプリケーション内でファイル名拡張子を使って識別できます。 たとえば、開発者が RSS 形式の XML を作成するハンドラーを作成した場合、アプリケーション内で .rss ファイル名拡張子をカスタム ハンドラーにバインドできます。 また、開発者は、特定のファイルにマップするハンドラーを作成し、これらのハンドラーをネイティブ モジュールとして、または ASP.NET の IHttpHandler インターフェイスとして、実装することもできます。

互換性

バージョン メモ
IIS 10.0 <handlers> 要素は、IIS 10.0 では変更されませんでした。
IIS 8.5 <handlers> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 <handlers> 要素は、IIS 8.0 では変更されませんでした。
IIS 7.5 <handlers> 要素は、IIS 7.5 では変更されませんでした。
IIS 7.0 <handlers> 要素は IIS 7.0 で導入されました。
IIS 6.0 該当なし

段取り

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

操作方法

統合モードで実行されている IIS 7 アプリケーションで ASP.NET ハンドラーのハンドラー マッピングを作成する方法

  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. [接続] ペインで、ネイティブ モジュールを追加するサーバー接続をクリックします。

  3. [ホーム] ペインで、[ハンドラー マッピング] をダブルクリックします。
    Screenshot of Home pane with Handler Mappings feature selected.

  4. [操作] ペインで、[マネージド ハンドラーの追加] をクリックします。

  5. [マネージド ハンドラーの追加] ダイアログ ボックスで、以下を指定します。

    • [要求パス]。 マップするファイル名またはファイル名拡張子。

    • Type。 マネージド ハンドラーの型 (クラス) の名前。 ハンドラーがアプリケーションの app_code または bin フォルダーで定義されている場合、その型の名前がドロップダウン リストに表示されます。

    • 名前。 わかりやすい名前。

      Screenshot shows Add Managed Handler dialog box with fields for Request Path, Type and Name.

  6. [OK] をクリックして [マネージド ハンドラーの追加] ダイアログ ボックスを閉じます。

FastCGI のハンドラー マッピングを作成する方法

  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. [接続] ペインで、FastCGI ハンドラー マッピングを構成するサーバー名をクリックします。

  3. [ホーム] ペインで、[ハンドラー マッピング] をダブルクリックします。
    Screenshot displays Server Home pane with Handler Mappings feature selected.

  4. [操作] ペインで、[モジュール マッピングの追加] をクリックします。

    Note

    次の手順を実行するには、指定するファイル パスまたはファイル名拡張子を実行するバイナリが既にインストールされている必要があります。 この例では、Microsoft の Web サイトから利用できる PHP の実装を使います。

  5. [要求パス] ボックスにファイル名拡張子 (.php など) を入力し、[モジュール] ドロップダウン リストで [FastCGIModule] をクリックし、[実行可能ファイル] ボックスにスクリプト エンジン (この例では、PHP-CGI.exe) へのパスを入力して、[OK] をクリックします。
    Screenshot of Add Module Mapping dialog box with Fast C G I Module selected from the Module drop down list.

  6. [モジュール マッピングの追加] ダイアログ ボックスで、[はい] をクリックします。
    Screenshot shows Add Module Mapping dialog box asking if you want to create a Fast C G I application for the executable.

構成

<handlers> 要素には <add> 要素のコレクションが含まれており、それぞれでアプリケーションのハンドラー マッピングが定義されています。 <add> 要素には、ハンドラーの名前、ハンドラーのマップ先のファイル名拡張子または URL パス マスク、ハンドラーの実装を指定するモジュール名または ASP.NET HTTP ハンドラーの種類、その他の設定が含まれています。

<handlers> 要素は、Web.config ファイルにおいてアプリケーションのレベルで構成します。

属性

属性 説明
accessPolicy 省略可能なフラグ属性。

ハンドラー コレクション全体に対して許可されるアクセスの種類を指定します。

accessPolicy 属性には、次のいずれかの値を指定できます。 既定値は、Read です。
Value 説明
Execute ディレクトリとファイルでの実行権限を必要とする、ハンドラー コレクション内のハンドラーを有効にします。
None ディレクトリまたはファイルへのアクセス権を必要とする、ハンドラー コレクション内のすべてのハンドラーを無効にします。
NoRemoteExecute ハンドラーがリモート要求を受信したときに、ハンドラー コレクション内のハンドラーが実行可能ファイルを実行できないようにします。
NoRemoteRead ハンドラーがリモート要求を受信したときに、ハンドラー コレクション内のハンドラーがファイルを読み取れないようにします。
NoRemoteScript ハンドラーがリモート要求を受信したときに、ハンドラー コレクション内のハンドラーがスクリプトを実行できないようにします。
NoRemoteWrite ハンドラーがリモート要求を受信したときに、ハンドラー コレクション内のハンドラーがファイルを作成または変更できないようにします。
Read ディレクトリとファイルへの読み取りアクセス権を必要とする、ハンドラー コレクション内のハンドラーを有効にします。
Script ディレクトリまたはファイルに対するスクリプト権限を必要とする、ハンドラー コレクション内のハンドラーを有効にします。
Source ソース コードへの読み取りアクセス権 (Read フラグと共に) またはソース コードへの書き込みアクセス権 (Write フラグと共に) を必要とする、ハンドラー コレクション内のハンドラー有効にします。
Write ディレクトリとファイルへの書き込みアクセス権を必要とする、ハンドラー コレクション内のハンドラーを有効にします。

子要素

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

ハンドラーのコレクションにハンドラーを追加します。
clear 省略可能な要素です。

ハンドラー コレクションからハンドラーへのすべての参照を削除します。
remove 省略可能な要素です。

ハンドラー コレクションからハンドラーへの参照を削除します。

構成サンプル

次の例には、ハンドラー マッピングを定義する 2 つの <add> 要素が含まれています。 1 番目の <add> 要素では、IIS 7 統合モードで実行されている Web アプリケーションの SampleHandler ハンドラーを定義しています。 ハンドラー アセンブリを Web アプリケーションの app_code ディレクトリに追加する場合、type 属性の値にアセンブリ名を含める必要はありません。 2 番目の <add> 要素では、FastCGI モジュールを使う PHP 要求のマッピングを定義しています。

<handlers>
   <add name="SampleHandler" verb="*" 
      path="SampleHandler.new" 
      type="SampleHandler, SampleHandlerAssembly" 
      resourceType="Unspecified" />
   <add name="PHP-FastCGI" verb="*" 
      path="*.php" 
      modules="FastCgiModule"
      scriptProcessor="c:\php\php-cgi.exe" 
      resourceType="Either" />
</handlers>

サンプル コード

次の例では、PHP モジュールの FastCGI マッピングを追加してから、PHP 要求を処理するハンドラーを Contoso Web サイトに追加します。

AppCmd.exe

appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='c:\php\php-cgi.exe']" /commit:apphost

appcmd.exe set config "Contoso" -section:system.webServer/handlers /+"[name='PHP-FastCGI',path='*.php',verb='GET,HEAD,POST',modules='FastCgiModule',scriptProcessor='c:\php\php-cgi.exe',resourceType='Either']"

Note

この 2 番目の例では、特定の URL に対する SampleHandler.new という名前の新しい ASP.NET ハンドラー マッピングを、Web アプリケーションに追加する方法を示します。

appcmd.exe set config /section:system.webServer/handlers /+[name=SampleHandler',path='SampleHandler.new',verb='*',type='SampleHandler']

C#

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

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration appHostConfig = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection fastCgiSection = appHostConfig.GetSection("system.webServer/fastCgi");
         ConfigurationElementCollection fastCgiCollection = fastCgiSection.GetCollection();
         ConfigurationElement applicationElement = fastCgiCollection.CreateElement("application");
         applicationElement["fullPath"] = @"c:\php\php-cgi.exe";
         fastCgiCollection.Add(applicationElement);

         Configuration webConfig = serverManager.GetWebConfiguration("Contoso");
         ConfigurationSection handlersSection = webConfig.GetSection("system.webServer/handlers");
         ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();
         ConfigurationElement addElement = handlersCollection.CreateElement("add");
         addElement["name"] = @"PHP-FastCGI";
         addElement["path"] = @"*.php";
         addElement["verb"] = @"GET,HEAD,POST";
         addElement["modules"] = @"FastCgiModule";
         addElement["scriptProcessor"] = @"c:\php\php-cgi.exe";
         addElement["resourceType"] = @"Either";
         handlersCollection.AddAt(0, 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 appHostConfig As Configuration = serverManager.GetApplicationHostConfiguration
      Dim fastCgiSection As ConfigurationSection = appHostConfig.GetSection("system.webServer/fastCgi")
      Dim fastCgiCollection As ConfigurationElementCollection = fastCgiSection.GetCollection
      Dim applicationElement As ConfigurationElement = fastCgiCollection.CreateElement("application")
      applicationElement("fullPath") = "c:\php\php-cgi.exe"
      fastCgiCollection.Add(applicationElement)

      Dim webConfig As Configuration = serverManager.GetWebConfiguration("Contoso")
      Dim handlersSection As ConfigurationSection = webConfig.GetSection("system.webServer/handlers")
      Dim handlersCollection As ConfigurationElementCollection = handlersSection.GetCollection
      Dim addElement As ConfigurationElement = handlersCollection.CreateElement("add")
      addElement("name") = "PHP-FastCGI"
      addElement("path") = "*.php"
      addElement("verb") = "GET,HEAD,POST"
      addElement("modules") = "FastCgiModule"
      addElement("scriptProcessor") = "c:\php\php-cgi.exe"
      addElement("resourceType") = "Either"
      handlersCollection.AddAt(0, addElement)

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');

adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var fastCgiSection = adminManager.GetAdminSection("system.webServer/fastCgi", "MACHINE/WEBROOT/APPHOST");
var fastCgiCollection = fastCgiSection.Collection;
var applicationElement = fastCgiCollection.CreateNewElement("application");
applicationElement.Properties.Item("fullPath").Value = "c:\\php\\php-cgi.exe";
fastCgiCollection.AddElement(applicationElement);
adminManager.CommitChanges();

adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso";
var handlersSection = adminManager.GetAdminSection("system.webServer/handlers", "MACHINE/WEBROOT/APPHOST/Contoso");
var handlersCollection = handlersSection.Collection;
var addElement = handlersCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "PHP-FastCGI";
addElement.Properties.Item("path").Value = "*.php";
addElement.Properties.Item("verb").Value = "GET,HEAD,POST";
addElement.Properties.Item("modules").Value = "FastCgiModule";
addElement.Properties.Item("scriptProcessor").Value = "c:\\php\\php-cgi.exe";
addElement.Properties.Item("resourceType").Value = "Either";
handlersCollection.AddElement(addElement, 0);
adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")

adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set fastCgiSection = adminManager.GetAdminSection("system.webServer/fastCgi", "MACHINE/WEBROOT/APPHOST")
Set fastCgiCollection = fastCgiSection.Collection
Set applicationElement = fastCgiCollection.CreateNewElement("application")
applicationElement.Properties.Item("fullPath").Value = "c:\php\php-cgi.exe"
fastCgiCollection.AddElement applicationElement
adminManager.CommitChanges()

adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso"
Set handlersSection = adminManager.GetAdminSection("system.webServer/handlers", "MACHINE/WEBROOT/APPHOST/Contoso")
Set handlersCollection = handlersSection.Collection
Set addElement = handlersCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "PHP-FastCGI"
addElement.Properties.Item("path").Value = "*.php"
addElement.Properties.Item("verb").Value = "GET,HEAD,POST"
addElement.Properties.Item("modules").Value = "FastCgiModule"
addElement.Properties.Item("scriptProcessor").Value = "c:\php\php-cgi.exe"
addElement.Properties.Item("resourceType").Value = "Either"
handlersCollection.AddElement addElement, 0
adminManager.CommitChanges()