筛选器 <filter>
概述
<isapiFilters>
集合的 <filter>
元素将 ISAPI 筛选器配置为处理客户端请求数据或服务器响应数据。
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <filter> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <filter> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | <filter> 元素在 IIS 8.0 中未进行修改。 |
IIS 7.5 | <filter> 元素在 IIS 7.5 中未进行修改。 |
IIS 7.0 | IIS 7.0 中引入了 <isapiFilters> 集合的 <filter> 元素。 |
IIS 6.0 | <isapiFilters> 集合取代了 IIS 6.0 的 FilterEnableCache 和 FilterPath 元数据库属性。 |
安装
要使用 <isapiFilters>
元素,必须在 IIS 7 及更高版本服务器上安装 ISAPI 筛选器模块。 为此,请按照以下步骤操作。
Windows Server 2012 或 Windows Server 2012 R2
- 在任务栏上,单击 “服务器管理器”。
- 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
- 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
- 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“应用程序开发”,然后选择“ISAPI 筛选器”。 单击 “下一步” 。
。 - 在“选择功能”页上,单击“下一步”。
- 在“确认安装选择”页上,单击“安装”。
- 在“结果” 页面中单击“关闭” 。
Windows 8 或 Windows 8.1
- 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
- 依次展开“Internet 信息服务”、“万维网服务”和“应用程序开发功能”,然后选择“ISAPI 文件”。
- 单击“确定”。
- 单击“关闭” 。
Windows Server 2008 或 Windows Server 2008 R2
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
- 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
- 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
- 在“添加角色服务向导”的“选择角色服务”页上,选择“ISAPI 筛选器”,然后单击“下一步”。
- 在“确认安装选择”页中,单击“安装”。
- 在“结果” 页面中单击“关闭” 。
Windows Vista 或 Windows 7
- 在任务栏上,单击“开始”,然后单击“控制面板”。
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
- 在“Windows 功能”对话框中,依次展开“Internet Information Services”、“万维网服务”、“应用程序开发功能”。
- 选择“ISAPI 筛选器”,然后单击“确定”。
操作方式
如何添加 ISAPI 筛选器
打开 Internet Information Services (IIS) 管理器:
如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:
- 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”。
如果使用的是 Windows 8 或 Windows 8.1:
- 按住 Windows 键,按字母 X,然后单击“控制面板”。
- 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”。
如果使用的是 Windows Vista 或 Windows 7:
- 在任务栏上,单击“开始”,然后单击“控制面板”。
- 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
在“连接”窗格中,转到要为其配置 ISAPI 筛选器的连接或站点。
在“操作”窗格中,单击“添加...”。
在“添加 ISAPI 筛选器”对话框的“筛选器名称”文本框中,键入 ISAPI 筛选器的友好名称。
在“可执行文件”框中,键入 ISAPI 筛选器文件位置的文件系统路径,或单击省略号 (...) 导航到包含 ISAPI 筛选器文件的文件夹,然后单击“确定”。
配置
特性
属性 | 说明 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
enableCache |
可选布尔属性。 指定为筛选的服务器响启用 (true) 还是禁用 (false) HTTP.sys 缓存。 默认值为 false 。 |
||||||||||||||
enabled |
可选布尔属性。 指定是启用 (true) 还是禁用 (false) 已安装的筛选器。 默认值为 true 。 |
||||||||||||||
name |
必需的字符串属性。 指定 ISAPI 筛选器的唯一名称。 |
||||||||||||||
path |
必需的字符串属性。 指定 ISAPI 筛选器 .dll 文件的完整物理路径。 |
||||||||||||||
preCondition |
可选的字符串属性。 指定 ISAPI 筛选器将在哪些条件下运行。 preCondition 属性的值可以是下列其中一个或多个。 如果指定多个值,请使用逗号分隔值 (,)。
|
子元素
无。
配置示例
以下配置示例将名为 SalesQueryIsapi 的 ISAPI 筛选器添加到网站或应用程序。 该示例命名并启用具有 name 和 enabled 属性的 ISAPI 筛选器。 它还使用 enableCache 属性来禁用 HTTP.sys 缓存,并使用 path 属性来指定 ISAPI DLL 的位置。
<configuration>
<system.webServer>
<isapiFilters>
<filter
name="SalesFilter"
enabled="true"
enableCache="false"
path="C:\Inetpub\filters\SalesFilter.dll" />
</isapiFilters>
</system.webServer>
</configuration>
代码示例
以下示例在服务器上配置名为 SalesQueryIsapi 的 ISAPI 筛选器。 每个示例都使用name 属性指定 ISAPI 筛选器的名称,使用 enableCache 属性禁用 HTTP.sys 缓存,使用 path 属性指定 ISAPI DLL 的位置。
AppCmd.exe
appcmd.exe set config -section:system.webServer/isapiFilters /+"[name='SalesQueryIsapi',path='c:\Inetpub\www.contoso.com\filters\SalesQueryIsapi.dll',enabled='True',enableCache='True']" /commit:apphost
注意
使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost
。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。
C#
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample
{
private static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection isapiFiltersSection = config.GetSection("system.webServer/isapiFilters");
ConfigurationElementCollection isapiFiltersCollection = isapiFiltersSection.GetCollection();
ConfigurationElement filterElement = isapiFiltersCollection.CreateElement("filter");
filterElement["name"] = @"SalesQueryIsapi";
filterElement["path"] = @"c:\Inetpub\www.contoso.com\filters\SalesQueryIsapi.dll";
filterElement["enabled"] = true;
filterElement["enableCache"] = true;
isapiFiltersCollection.Add(filterElement);
serverManager.CommitChanges();
}
}
}
VB.NET
Imports System
Imports System.Text
Imports Microsoft.Web.Administration
Module Sample
Sub Main()
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetApplicationHostConfiguration
Dim isapiFiltersSection As ConfigurationSection = config.GetSection("system.webServer/isapiFilters")
Dim isapiFiltersCollection As ConfigurationElementCollection = isapiFiltersSection.GetCollection
Dim filterElement As ConfigurationElement = isapiFiltersCollection.CreateElement("filter")
filterElement("name") = "SalesQueryIsapi"
filterElement("path") = "c:\Inetpub\www.contoso.com\filters\SalesQueryIsapi.dll"
filterElement("enabled") = True
filterElement("enableCache") = True
isapiFiltersCollection.Add(filterElement)
serverManager.CommitChanges()
End Sub
End Module
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var isapiFiltersSection = adminManager.GetAdminSection("system.webServer/isapiFilters", "MACHINE/WEBROOT/APPHOST");
var isapiFiltersCollection = isapiFiltersSection.Collection;
var filterElement = isapiFiltersCollection.CreateNewElement("filter");
filterElement.Properties.Item("name").Value = "SalesQueryIsapi";
filterElement.Properties.Item("path").Value = "c:\\Inetpub\\www.contoso.com\\filters\\SalesQueryIsapi.dll";
filterElement.Properties.Item("enabled").Value = true;
filterElement.Properties.Item("enableCache").Value = true;
isapiFiltersCollection.AddElement(filterElement);
adminManager.CommitChanges();
VBScript
Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set isapiFiltersSection = adminManager.GetAdminSection("system.webServer/isapiFilters", "MACHINE/WEBROOT/APPHOST")
Set isapiFiltersCollection = isapiFiltersSection.Collection
Set filterElement = isapiFiltersCollection.CreateNewElement("filter")
filterElement.Properties.Item("name").Value = "SalesQueryIsapi"
filterElement.Properties.Item("path").Value = "c:\\Inetpub\\www.contoso.com\\filters\\SalesQueryIsapi.dll"
filterElement.Properties.Item("enabled").Value = True
filterElement.Properties.Item("enableCache").Value = True
isapiFiltersCollection.AddElement filterElement
adminManager.CommitChanges()