<处理程序处理程序>
概述
该 <handlers>
元素定义为特定文件扩展名或 URL 注册的处理程序。
处理程序是 Internet Information Services (IIS) 组件,这些组件配置为处理对特定内容的请求,通常是为请求资源生成响应。 例如,ASP.NET 网页是一种类型的处理程序。 可以使用处理程序来处理任何需要将信息返回到不是静态文件的用户的资源的请求。 IIS 提供了几种不同的处理程序类型来支持静态文件、通用网关接口 (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 处理程序创建处理程序映射
打开 Internet Information Services (IIS) 管理器:
如果使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在任务栏上,单击服务器管理器,单击“工具”,然后单击“Internet Information Services” (IIS) 管理器。
如果使用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) 管理器。
在“ 连接 ”窗格中,单击要向其添加本机模块的服务器连接。
在 “开始 ”窗格中,双击 “处理程序映射”。
在“ 操作 ”窗格中,单击“ 添加托管处理程序...”
在“ 添加托管处理程序 ”对话框中,指定以下内容:
请求路径。 要映射的文件名或文件扩展名。
类型。 托管处理程序的类型 (类) 名称。 如果在应用程序的app_code或 bin 文件夹中定义处理程序,则其类型名称将显示在下拉列表中。
Name。 描述性名称。
单击 “确定 ”关闭 “添加托管处理程序 ”对话框。
如何创建 FastCGI 处理程序映射
打开 Internet Information Services (IIS) 管理器:
如果使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在任务栏上,单击服务器管理器,单击“工具”,然后单击“Internet Information Services” (IIS) 管理器。
如果使用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) 管理器。
在“ 连接 ”窗格中,单击要为其配置 FastCGI 处理程序映射的服务器名称。
在 “开始 ”窗格中,双击 “处理程序映射”。
在 “操作 ”窗格中,单击“ 添加模块映射...”
注意
若要执行后续步骤,必须已安装二进制文件,该二进制文件将执行指定的文件路径或文件扩展名。 此示例使用 Microsoft 网站提供的 PHP 实现。
在“请求路径”框中键入文件扩展名,单击“模块”下拉列表中的 FastCGIModule,在此示例中键入脚本引擎的路径 (,在“可执行文件”框中PHP-CGI.exe) ,然后单击“确定”。
在“ 添加模块映射 ”对话框中,单击“ 是”。
配置
该 <handlers>
元素包含一组 <add>
元素,其中每个元素定义应用程序的处理程序映射。 该 <add>
元素包含处理程序的名称、处理程序映射到的文件扩展名或 URL 路径掩码,以及指定处理程序实现和其他设置的模块名称或 ASP.NET HTTP 处理程序类型。
可以在Web.config文件中的应用程序级别配置 <handlers>
元素。
属性
特性 | 说明 | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accessPolicy |
可选标志属性。 指定整个处理程序集合的允许访问类型。 accessPolicy 属性可以是下列可能值之一。 默认为 Read 。
|
子元素
元素 | 说明 |
---|---|
add |
可选元素。 将处理程序添加到处理程序集合。 |
clear |
可选元素。 从处理程序集合中删除对处理程序的所有引用。 |
remove |
可选元素。 从处理程序集合中删除对处理程序的引用。 |
配置示例
以下示例包含两 <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 映射,然后在 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']"
注意
第二个示例演示如何为 Web 应用程序添加名为 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()