为 Internet Information Services (IIS) 7 添加 Modules <add> 元素;

概述

<modules> 集合的 <add> 元素向 Internet Information Services (IIS) 7 的模块列表添加模块。

兼容性

版本 说明
IIS 10.0 <add> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <add> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <add> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <add> 元素未在 IIS 7.5 中进行修改。
IIS 7.0 IIS 7.0 中引入了 <modules> 集合的 <add> 元素。
IIS 6.0 空值

安装

在 IIS 7 的默认安装中包含 <modules> 集合的 <add> 元素。

操作方式

如何将托管模块添加到应用程序

  1. 打开 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 Information Services (IIS) 管理器”。
  2. 在“连接”窗格中,展开服务器名称,展开“站点”,然后转至要在其中添加托管模块的网站或应用程序。

  3. 在“主页”窗格中,双击“模块”
    “主页”窗格的屏幕截图,其中突出显示了“模块”。

  4. 在“操作”窗格中,单击“添加托管模块”。

  5. “添加托管模块”对话框中,在“名称”框中输入托管模块的名称,然后在“类型”框中输入或选择模块的 .NET Framework 完全限定类型。

  6. 如果希望模块仅响应托管请求,请选择“仅对 ASP.NET 应用程序或托管处理程序的请求调用”。
    “添加托管模块”对话框的图像,其中显示了“名称”和“类型”框,其中仅针对对 S P dot Net 应用程序或托管处理程序选项的请求调用。

  7. 单击“确定”。

配置

特性

属性 说明
name 必需的字符串属性。

指定 Web 服务器上托管模块的唯一名称。
preCondition 可选的字符串属性。 指定模块将在哪些条件下运行。

preCondition 属性的值可以是下列其中一个或多个。 如果指定多个值,请使用逗号分隔值 (,)。

说明
bitness32 当模块是 32 位 .dll 文件时,请指定 bitness32 值。 在 64 位操作系统上,IIS 应仅加载在 WOW64 模式下运行的工作进程(32 位模拟)的处理程序。
bitness64 当模块是 64 位 .dll 文件时,请指定 bitness64 值。 IIS 应仅加载在 64 位模式下运行的工作进程的处理程序。
integratedMode 当模块仅应响应配置为使用集成请求处理管道的应用程序池中的请求时,请指定 integratedMode 值。
ISAPIMode 当模块仅应响应配置为使用经典模式的应用程序池中的请求时,请指定 ISAPIMode 值。
managedHandler 当模块仅处理托管资源(如 .aspx 文件)的请求,并且不应响应非托管资源(如 .html 文件)的请求时,请指定 managedHandler 值。
runtimeVersionv1.1 当模块仅应响应配置为使用 .NET Framework 版本 1.1 的应用程序池中的请求时,请指定 runtimeVersionv1.1 值。
runtimeVersionv2.0 当模块仅应响应配置为使用 .NET Framework 版本 2.0 的应用程序池中的请求时,请指定 runtimeVersionv2.0 值。
runtimeVersionv4.0 模块应仅响应配置为使用 .NET Framework 版本 4.0 的应用程序池中的请求时,请指定 runtimeVersionv4.0 值。
type 可选的字符串属性。

指定托管模块的托管类型。 类型属性不适用于本机模块。

子元素

无。

配置示例

该示例为 IIS 7 集成模式下运行的 Web 应用程序配置模块。

<configuration>
   <system.webServer>
      <modules>
         <add name="Header" type="Contoso.ShoppingCart.Header"/>
      </modules>
   </system.webServer>
</configuration>

代码示例

注意

本文档中的示例说明如何使用已存储在 .NET 全局程序集缓存 (GAC) 中的托管代码程序集。 在使用这些示例中的代码部署你自己的程序集之前,你需要从 GAC 检索程序集信息。 为此,请按照以下步骤操作:

  • 在 Windows 资源管理器中打开 C:\Windows\assembly 路径,其中 C: 是操作系统驱动器。
  • 找到你的程序集。
  • 右键单击程序集,然后单击“属性”
  • 复制“区域性”值,例如“Neutral”
  • 复制“版本”号,例如“1.0.0.0”
  • 复制“公钥令牌”值,例如“426f62526f636b73”
  • 单击“取消” 。

以下代码示例为名为 Contoso 的网站启用托管模块。 name 属性定义模块的名称 CartHeader,type 属性定义模块的托管类型,preCondition 属性定义 IIS 仅针对托管请求调用模块。

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/modules /+"[name='CartHeader',type='Contoso.ShoppingCart.Header',preCondition='managedHandler']"

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.GetWebConfiguration("Contoso");
         ConfigurationSection modulesSection = config.GetSection("system.webServer/modules");
         ConfigurationElementCollection modulesCollection = modulesSection.GetCollection();

         ConfigurationElement addElement = modulesCollection.CreateElement("add");
         addElement["name"] = @"CartHeader";
         addElement["type"] = @"Contoso.ShoppingCart.Header";
         addElement["preCondition"] = @"managedHandler";
         modulesCollection.Add(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 config As Configuration = serverManager.GetWebConfiguration("Contoso")
      Dim modulesSection As ConfigurationSection = config.GetSection("system.webServer/modules")
      Dim modulesCollection As ConfigurationElementCollection = modulesSection.GetCollection

      Dim addElement As ConfigurationElement = modulesCollection.CreateElement("add")
      addElement("name") = "CartHeader"
      addElement("type") = "Contoso.ShoppingCart.Header"
      addElement("preCondition") = "managedHandler"
      modulesCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso";
var modulesSection = adminManager.GetAdminSection("system.webServer/modules", "MACHINE/WEBROOT/APPHOST/Contoso");
var modulesCollection = modulesSection.Collection;

var addElement = modulesCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "CartHeader";
addElement.Properties.Item("type").Value = "Contoso.ShoppingCart.Header";
addElement.Properties.Item("preCondition").Value = "managedHandler";
modulesCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso"
Set modulesSection = adminManager.GetAdminSection("system.webServer/modules", "MACHINE/WEBROOT/APPHOST/Contoso")
Set modulesCollection = modulesSection.Collection

Set addElement = modulesCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "CartHeader"
addElement.Properties.Item("type").Value = "Contoso.ShoppingCart.Header"
addElement.Properties.Item("preCondition").Value = "managedHandler"
modulesCollection.AddElement addElement

adminManager.CommitChanges()