新增處理常式 < 新增>
概觀
<add>
集合的 <handlers>
元素會將處理常式新增至 Internet Information Services (IIS) 7 的 HTTP 處理常式清單。
相容性
版本 | 備註 |
---|---|
IIS 10.0 | 未在 IIS 10.0 中修改專案 <add> 。 |
IIS 8.5 | 未在 IIS 8.5 中修改專案 <add> 。 |
IIS 8.0 | 未在 IIS 8.0 中修改專案 <add> 。 |
IIS 7.5 | 未在 IIS 7.5 中修改專案 <add> 。 |
IIS 7.0 | <add> 集合的 <handlers> 元素是在 IIS 7.0 中引進。 |
IIS 6.0 | N/A |
安裝程式
<add>
集合的 <handlers>
元素包含在 IIS 7 的預設安裝中。
作法
如何在整合模式中執行的 IIS 7 應用程式中,為 ASP.NET 處理常式建立處理常式對應
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8 或 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
如果您使用 Windows Server 2008 或 Windows Server 2008 R2:
- 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
如果您使用 Windows Vista 或 Windows 7:
- 在工作列上,按一下 [開始],然後按一下[主控台]。
- 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager。
在 [ 連線] 窗格中,按一下您要新增原生模組的伺服器連線。
在 [ 首頁] 窗格中,按兩下 [處理常式對應]。
在 [ 動作 ] 窗格上,按一下 [ 新增受管理處理程式...
在 [ 新增 Managed 處理常式 ] 對話方塊中,指定下列專案:
要求路徑。 要對應的檔案名或副檔名。
類型。 型別 (類別) Managed 處理常式的名稱。 如果處理常式是在應用程式的app_code或 bin 資料夾中定義,則其類型名稱會出現在下拉式清單中。
名稱。 描述性名稱。
按一下 [確定 ] 以關閉 [ 新增 Managed 處理常式 ] 對話方塊。
如何建立 FastCGI 處理常式對應
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8 或 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
如果您使用 Windows Server 2008 或 Windows Server 2008 R2:
- 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
如果您使用 Windows Vista 或 Windows 7:
- 在工作列上,按一下 [開始],然後按一下[主控台]。
- 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager。
在 [ 連線] 窗格中,按一下您要設定 FastCGI 處理常式對應的伺服器名稱。
在 [ 首頁] 窗格中,按兩下 [處理常式對應]。
在 [ 動作 ] 窗格中,按一下 [ 新增模組對應...
注意
若要讓後續步驟能夠運作,您必須已安裝二進位檔,以執行您指定的檔案路徑或副檔名。 此範例使用可從 Microsoft 網站取得的 PHP 實作。
在 [要求路徑] 方塊中輸入副檔名,例如.php,按一下[模組] 下拉式清單中的[FastCGIModule],在此範例中輸入腳本引擎 (的路徑,PHP-CGI.exe) [可執行檔] 方塊中,然後按一下 [確定]。
在 [ 新增模組對應 ] 對話方塊中,按一下 [ 是]。
組態
屬性
屬性 | 描述 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
allowPathInfo |
選擇性的 Boolean 屬性。 指定處理常式是否處理 URI 中的完整路徑資訊,例如 contoso/marketing/imageGallery.aspx。 如果值為 true,處理常式會處理完整路徑 contoso/marketing/imageGallery。 如果值為 false,處理常式只會處理路徑 /imageGallery 的最後一個區段。 預設值是 false 。 |
||||||||||||||
modules |
選擇性字串屬性。 指定您要對應副檔名之檔案名或檔案名的模組名稱。 如果您指定多個值,請使用逗號分隔值 (,) 。 預設值是 ManagedPipelineHandler 。 |
||||||||||||||
name |
必要的字串屬性。 指定處理常式對應的唯一名稱。 |
||||||||||||||
path |
必要的字串屬性。 指定套用處理程式對應的檔案名或副檔名。 |
||||||||||||||
preCondition |
選擇性字串屬性。 指定要執行處理常式的條件。 preCondition屬性可以是下列一或多個可能的值。 如果您指定多個值,請使用逗號分隔值 (,) 。
|
||||||||||||||
requireAccess |
選擇性列舉屬性。 指定處理常式對資源所需的存取類型。 requireAccess屬性可以是下列一或多個可能的值。 如果您指定多個值,請使用逗號分隔值 (,) 。 預設值是 Script 。
|
||||||||||||||
resourceType |
選擇性字串屬性。 指定處理常式對應所套用的資源類型。 resourceType屬性可以是下列其中一個可能的值。 預設值是 Unspecified 。
|
||||||||||||||
responseBufferLimit |
選擇性 uint 屬性。 指定要求處理常式之回應緩衝區的大小上限,以位元組為單位。 預設值為 4194304 位元組。 |
||||||||||||||
scriptProcessor |
選擇性字串屬性。 指定處理要求的 ISAPI 擴充功能.dll檔案或一般閘道介面 (CGI) .exe檔案的實體路徑。 scriptProcessor 屬性僅適用于腳本對應處理常式對應。 當您將處理常式對應至 ISAPI 延伸模組時,您必須為 modules 屬性指定 ISAPIModule。 當您將處理常式對應至 CGI 檔案時,您必須為 modules 屬性指定 CGIModule。 |
||||||||||||||
type |
選擇性字串屬性。 指定 Managed 處理常式的命名空間路徑。 只有 Managed 處理常式才需要類型屬性。 |
||||||||||||||
verb |
必要的字串屬性。 指定處理常式對應適用的 HTTP 動詞。 |
子元素
無。
組態範例
下列範例包含兩 <add>
個定義處理常式對應的專案。 第一個專案 <add>
會定義在 IIS 7 整合模式中執行的 Web 應用程式的 SampleHandler 處理常式。 如果您將處理常式元件新增至 Web 應用程式的app_code目錄,則不需要在 類型 屬性的值中包含元件名稱。 第二 <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 網站上新增處理常式。
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 處理常式對應新增至 Web 應用程式的特定 URL。
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()