概觀
該 <handlers> 元素定義了特定檔案副檔名或 URL 註冊的處理器。
處理器是網際網路資訊服務(IIS)的元件,負責處理特定內容的請求,通常用於產生請求資源的回應。 例如,ASP.NET 網頁就是處理程序的一種。 你可以使用處理器來處理任何需要回傳資訊給使用者的資源請求,但這些請求不是靜態檔案。 IIS 提供多種不同的處理程式類型,以支援靜態檔案、通用閘道介面(CGI)應用程式、FastCGI 應用程式及網際網路伺服器 API(ISAPI)擴充功能。 ASP.NET 也透過 HTTP 處理器介面支援額外的處理器類型。
開發者可以建立自訂處理器來執行特殊處理,這些處理方式可透過應用程式中的檔名副檔名來識別。 舉例來說,如果開發者建立了一個能產生 RSS 格式 XML 的處理器,你可以將應用程式中的 .rss 檔名副檔名綁定到自訂處理器上。 開發者也可以建立對應特定檔案的處理器,並將這些處理器實作為原生模組,或作為 ASP.NET IHttpHandler 介面的實作。
Compatibility
| 版本 | 註釋 |
|---|---|
| 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 | N/A |
設定
該 <handlers> 元素包含在 IIS 7 的預設安裝中。
操作指南
如何在 IIS 7 應用程式中以整合模式運行時,為 ASP.NET 處理程序建立處理程序映射
開放 網際網路資訊服務(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)管理員」。
在 Connections 面板中,點選你想新增原生模組的伺服器連線。
在 主畫面 中,雙擊「 處理器映射」。
在 動作 選單中,點選 新增管理處理程序...
在 「新增受管理處理器 」對話框中,請指定以下內容:
請求路徑。 檔案名稱或副檔名來映射。
打字。 管理處理器的型別(類別)名稱。 如果處理器是在應用程式的app_code或 bin 資料夾中定義的,其類型名稱會出現在下拉選單中。
名稱. 一個描述性的名字。
點擊 確定 以關閉 新增管理處理器 對話框。
如何建立 FastCGI 處理器映射
開放 網際網路資訊服務(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)管理員」。
在 連接 面板中,點選你想設定 FastCGI 處理器映射的伺服器名稱。
在 主畫面 中,雙擊「 處理器映射」。
在 動作 面板中,點選 新增模組映射...
備註
接下來的步驟要順利進行,你必須已經安裝了執行你指定的檔案路徑或副檔名的二進位檔。 此範例使用了 Microsoft 官網提供的 PHP 實作。
在請求路徑框輸入副檔名,例如 as.php,在模組下拉選單點選 FastCGIModule,在執行檔框輸入腳本引擎的路徑(此例為 PHP-CGI.exe),然後點擊確定。
在 「新增模組映射 」對話框中,點選 「是」。
設定
該 <handlers> 元素包含一 <add> 組元素,每個元素定義應用程式的處理埠映射。 該 <add> 元素包含處理器名稱、處理器所映射的檔名副檔名或 URL 路徑遮罩,以及模組名稱 ASP.NET 或指定處理器實作的 HTTP 處理程式類型,還有其他設定。
你可以在 Web.config 檔案的應用程式層級設定元素 <handlers> 。
Attributes
| Attribute | Description | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accessPolicy |
可選的旗標屬性。 指定整個處理器集合的存取類型層級。 accessPolicy 屬性可以是以下可能的值之一。 預設值為 Read。
|
備註
屬性 accessPolicy 並非可強制執行的安全政策,而是一種過濾機制,根據設定時的屬性決定執行哪些處理器 requiredAccess 。 過濾處理程序仍是配置的一部分,且可被初始化,儘管不會根據此屬性被選擇執行。
子專案
| 元素 | Description |
|---|---|
add |
可選元素。 將一名操作員加入操作員的集合中。 |
clear |
可選元素。 移除所有關於處理器的引用,從處理器集合中移除。 |
remove |
可選元素。 移除處理者集合中對處理程序的引用。 |
組態範例
以下範例包含兩個 <add> 定義處理器映射的元素。 第一個 <add> 元素定義了 IIS 7 整合模式下執行的網頁應用程式的 SampleHandler 處理器。 如果你將處理程序組件加入網頁應用程式的 app_code 目錄,則不需要在 type 屬性的值中包含組合名稱。 第二個 <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 映射,並在 Contoso 網站上新增處理 PHP 請求的處理器。
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']"
備註
第二個範例展示了如何為特定網址新增一個名為 SampleHandler.new 的新 ASP.NET 處理器映射,以支援網頁應用程式。
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()