ハンドラー <handlers>
- 概要
- 互換性
- セットアップ
- 方法
- 構成
- サンプル コード
※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。
概要
<handlers>
要素は、特定のファイル名拡張子または URL に対して登録されたハンドラーを定義します。
ハンドラーは、特定のコンテンツへの要求を処理するために、通常は要求リソースへの応答を生成するために構成されるインターネット インフォメーション サービス (IIS) のコンポーネントです。たとえば、ASP.NET Web ページはハンドラーの種類の 1 つです。静的ファイルではない情報をユーザーに返す必要があるリソースへの要求を処理するためにハンドラーを使用できます。IIS では、静的ファイル、CGI (Common Gateway Interface) アプリケーション、FastCGI アプリケーション、および インターネット サーバー API (ISAPI : Internet Server API) 拡張子をサポートする数種類のハンドラーを提供しています。また、ASP.NET が HTTP ハンドラー インターフェイス経由で他のハンドラーの種類に対するサポートを提供しています。
開発者は、アプリケーションでファイル名拡張子を使用して識別できる、特別の処理を実行するためのカスタム ハンドラーを作成できます。たとえば、開発者が RSS 形式の XML を作成するハンドラーを作成した場合、アプリケーションで .rss ファイル名拡張子をカスタム ハンドラーにバインドできます。さらに、開発者は、特定のファイルにマップするハンドラーを作成し、これらのハンドラーをネイティブ モジュールとして、または ASP.NET IHttpHandler インターフェイスの実装として実装できます。
互換性
IIS 7.0 | IIS 6.0 | |
---|---|---|
注意 | <handlers> は IIS 7.0 で新たに導入された要素です。 |
なし |
セットアップ
<handlers>
要素は、IIS 7.0 の既定のインストールに含まれています。
方法
統合モードで実行されている IIS 7.0 アプリケーションで ASP.NET ハンドラーにマッピングするハンドラーを作成する方法
タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
[接続] ウィンドウで、ネイディブ モジュールを追加するサーバー接続をクリックします。
[ホーム] ウィンドウで [ハンドラー マッピング] をダブルクリックします。
[操作] ウィンドウで [マネージ ハンドラーの追加] をクリックします。
[マネージ ハンドラーの追加] ダイアログ ボックスで、次の項目を指定します。
- 要求パス。マップするファイル名またはファイル名拡張子です。
- 種類。マネージ ハンドラーの種類 (クラス) の名前です。ハンドラーが app_code またはアプリケーションの bin フォルダーで定義されている場合、その種類名がドロップダウン リストに表示されます。
- 名前。説明的な名前です。
[OK] をクリックして、[マネージ ハンドラーの追加] ダイアログ ボックスを閉じます。
FastCGI ハンドラー マッピングを作成する方法
タスク バーで [スタート] ボタンをクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
[接続] ウィンドウで、FastCGI ハンドラー マッピングを構成するサーバー名をクリックします。
[ホーム] ウィンドウで [ハンドラー マッピング] をダブルクリックします。
[操作] ウィンドウで [モジュール マップの追加] をクリックします。
注 : 次の手順の操作を行うには、指定するファイル パスまたはファイル名拡張子を実行するバイナリを既にインストールしている必要があります。この例では、Microsoft Web サイトで入手できる PHP 実装を使用します。
[要求パス] ボックスに「.php」などのファイル名拡張子を入力し、[モジュール] ドロップダウン リストで [FastCGIModule] をクリックし、[実行可能ファイル] ボックスでスクリプト エンジン (この例では、PHP-CGI.exe) へのパスを入力して、[OK] をクリックします。
[モジュール マップの追加] ダイアログ ボックスで [はい] をクリックします。
構成
<handlers>
要素には、さまざまな <add>
要素が含まれ、これらの各要素によって、アプリケーションのハンドラー マッピングが定義されます。<add>
要素には、ハンドラーの名前、ハンドラーがマップされるファイル名拡張子または URL パス マスク、およびハンドラー実装を指定するモジュール名または ASP.NET HTTP ハンドラーの種類が、他の設定と共に含まれます。
<handlers>
要素は、Web.config ファイル内で、アプリケーション レベルで構成できます。
属性
属性 | 説明 |
---|---|
accessPolicy |
オプションの flags 属性。 ハンドラー コレクション全体に許可されたアクセスの種類を指定します。 accessPolicy 属性には、次のいずれかの値を指定できます。既定値は Read です。---------------------------------------------------------------------------------------------- 値: Execute 説明:
値: None 説明:
値: NoRemoteExecute 説明:
値: NoRemoteRead 説明:
値: NoRemoteScript 説明:
値: NoRemoteWrite 説明:
値: Read 説明:
値: Script 説明:
値: Source 説明:
値: Write 説明:
|
子要素
要素 | 説明 |
---|---|
add |
オプションの要素。 ハンドラーのコレクションにハンドラーを追加します。 |
clear |
オプションの要素。 ハンドラー コレクションからハンドラーに対するすべての参照を削除します。 |
remove |
オプションの要素。 ハンドラー コレクションから 1 つのハンドラーに対する参照を削除します。 |
構成サンプル
次の例には、ハンドラー マッピングを定義する 2 つの <add>
要素が含まれています。最初の <add>
要素は、IIS 7.0 統合モードで実行している 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']"
注 : この 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()