共用方式為


新增處理常式 < 新增>

概觀

<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 處理常式建立處理常式對應

  1. 開啟 [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
  2. 在 [ 連線] 窗格中,按一下您要新增原生模組的伺服器連線。

  3. 在 [ 首頁] 窗格中,按兩下 [處理常式對應]。
    I S Manager 的螢幕擷取畫面,其中顯示預設網站首頁。[處理常式對應] 圖示會反白顯示。

  4. 在 [ 動作 ] 窗格上,按一下 [ 新增受管理處理程式...

  5. 在 [ 新增 Managed 處理常式 ] 對話方塊中,指定下列專案:

    • 要求路徑。 要對應的檔案名或副檔名。

    • 類型。 型別 (類別) Managed 處理常式的名稱。 如果處理常式是在應用程式的app_code或 bin 資料夾中定義,則其類型名稱會出現在下拉式清單中。

    • 名稱。 描述性名稱。

      [新增 Managed 處理常式] 對話方塊的螢幕擷取畫面。

  6. 按一下 [確定 ] 以關閉 [ 新增 Managed 處理常式 ] 對話方塊。

如何建立 FastCGI 處理常式對應

  1. 開啟 [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
  2. 在 [ 連線] 窗格中,按一下您要設定 FastCGI 處理常式對應的伺服器名稱。

  3. 在 [ 首頁] 窗格中,按兩下 [處理常式對應]。
    顯示 [伺服器首頁] 的 [I I S Manager] 視窗螢幕擷取畫面。[處理常式對應] 的圖示會反白顯示。

  4. 在 [ 動作 ] 窗格中,按一下 [ 新增模組對應...

    注意

    若要讓後續步驟能夠運作,您必須已安裝二進位檔,以執行您指定的檔案路徑或副檔名。 此範例使用可從 Microsoft 網站取得的 PHP 實作。

  5. 在 [要求路徑] 方塊中輸入副檔名,例如.php,按一下[模組] 下拉式清單中的[FastCGIModule],在此範例中輸入腳本引擎 (的路徑,PHP-CGI.exe) [可執行檔] 方塊中,然後按一下 [確定]。
    [新增模組對應] 對話方塊的螢幕擷取畫面。

  6. 在 [ 新增模組對應 ] 對話方塊中,按一下 [ ]。
    [新增模組對應資訊] 對話方塊的螢幕擷取畫面。

組態

屬性

屬性 描述
allowPathInfo 選擇性的 Boolean 屬性。

指定處理常式是否處理 URI 中的完整路徑資訊,例如 contoso/marketing/imageGallery.aspx。 如果值為 true,處理常式會處理完整路徑 contoso/marketing/imageGallery。 如果值為 false,處理常式只會處理路徑 /imageGallery 的最後一個區段。

預設值是 false
modules 選擇性字串屬性。

指定您要對應副檔名之檔案名或檔案名的模組名稱。 如果您指定多個值,請使用逗號分隔值 (,) 。

預設值是 ManagedPipelineHandler
name 必要的字串屬性。

指定處理常式對應的唯一名稱。
path 必要的字串屬性。

指定套用處理程式對應的檔案名或副檔名。
preCondition 選擇性字串屬性。

指定要執行處理常式的條件。

preCondition屬性可以是下列一或多個可能的值。 如果您指定多個值,請使用逗號分隔值 (,) 。
描述
bitness32 當處理常式是 32 位.dll檔案時,請指定 bitness32 值,IIS 應該只在 WOW64 模式中執行的背景工作進程載入處理常式, (32 位模擬) 在 64 位作業系統上執行。
bitness64 當處理常式是 64 位.dll檔案時,請指定 bitness64 值,而且 IIS 應該只針對以 64 位模式執行的背景工作進程載入處理常式。
integratedMode 當處理常式應該只回應設定為使用整合式要求處理管線的應用程式集區中要求時,請指定 integratedMode 值。
ISAPIMode 當處理常式應該只回應設定為使用傳統模式的應用程式集區中要求時,請指定 ISAPIMode 值。
runtimeVersionv1.1 當處理常式只回應設定為使用 .NET Framework 1.1 版的應用程式集區中要求時,請指定runtimeVersionv1.1值。
runtimeVersionv2.0 當處理常式只回應設定為使用 .NET Framework 2.0 版的應用程式集區中要求時,請指定runtimeVersionv2.0值。
requireAccess 選擇性列舉屬性。

指定處理常式對資源所需的存取類型。

requireAccess屬性可以是下列一或多個可能的值。 如果您指定多個值,請使用逗號分隔值 (,) 。 預設值是 Script
描述
執行 指定處理常式需要執行可執行檔的許可權。

數值為 4
None 指定處理常式沒有存取需求。

數值為 0
讀取 指定處理常式需要讀取權限。

數值為 1
指令碼 指定處理常式需要執行腳本的許可權。

數值為 3
寫入 指定處理常式需要寫入權限。

數值為 2
resourceType 選擇性字串屬性。

指定處理常式對應所套用的資源類型。

resourceType屬性可以是下列其中一個可能的值。 預設值是 Unspecified
描述
Directory 指定處理常式對應只適用于磁片上實體資料夾的要求。

數值為 1
Either 指定處理常式對應會套用至磁片上實體檔案或資料夾的要求。

數值為 2
File 指定處理常式對應僅適用于磁片上的實體檔案要求。

數值為 0
Unspecified 指定未指定對應類型。 不論要求是否對應至磁片上的實體檔案或資料夾,處理常式對應都會套用至要求。 當您將處理常式對應至磁片上不存在的檔案名或副檔名時,請使用此設定,例如 MyHandler.axd。

數值為 3
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()