Share via


Modules 元素 < 模組專案>

概觀

元素 <modules> 會定義為應用程式註冊的原生程式碼模組和 Managed 程式碼模組。 在 Internet Information Services (IIS) 要求管線處理過程中,IIS 會在每個要求上呼叫元素中列出的 <modules> 每個模組。 您通常會使用模組來實作自訂功能,例如安全性、統計資料和記錄,或自訂的內容處理,例如新增自訂頁首或頁尾。

專案 <modules> 包含專案的 <add> 集合。 每個元素都會定義應用程式的已啟用模組。 當您啟用模組時,可讓您為特定應用程式提供服務。

如果您想要啟用原生模組,您必須先在伺服器上加以安裝。 如需詳細資訊,請參閱< globalModules >元素。

您不需要安裝受控模組;您可以針對每個應用程式直接啟用它。 這可讓應用程式在應用程式的 Web.config 檔案中註冊它們,並在 /BIN 或 /App_Code 目錄中提供實作,直接將其包含在應用程式中。

相容性

版本 備註
IIS 10.0 <modules> IIS 10.0 中未修改專案。
IIS 8.5 <modules> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <modules>
IIS 7.5 <modules> 在 IIS 7.5 中修改專案。
IIS 7.0 專案 <modules> 是在 IIS 7.0 中引進的。
IIS 6.0 N/A

安裝程式

專案 <modules> 包含在 IIS 7 的預設安裝中。

作法

如何將受控模組新增至應用程式

  1. (IIS) 管理員開啟 Internet Information Services

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
  2. 在 [ 連線 ] 窗格中,展開伺服器名稱、[ 月臺],然後移至您要新增受控模組的網站或應用程式。

  3. 在 [ 首頁] 窗格中,按兩下 [ 模組]。
    預設網站首頁的螢幕擷取畫面。模組的圖示會反白顯示。

  4. 在 [動作] 窗格中,按一下 [ 新增受控模組]。

  5. 在 [新增受管理模組] 對話方塊中,于 [名稱] 方塊中輸入 Managed 模組的名稱,然後在 [類型] 方塊中輸入或選取模組 .NET Framework的完整類型。

  6. 如果您想要模組只回應 Managed 要求,請 選取 [僅針對要求叫用以 ASP.NET 應用程式或 Managed 處理常式 選項。
    [新增受控模組] 對話方塊的螢幕擷取畫面。

  7. 按一下 [確定]。

組態

<modules>您可以在ApplicationHost.config檔案中的伺服器層級,以及Web.config檔案中的應用層級設定 專案。

屬性

屬性 描述
runAllManagedModulesForAllRequests 選擇性布林值。

如果 所有 Managed 模組都可以處理所有要求,則為 True,即使要求不是受管理內容也一樣;否則為 false

注意: 在 ASP.NET 網站中, runAllManagedModulesForAllRequests 先前必須設定 true 的值以支援路由。 不過,使用 Service Pack 更新 IIS 7 之後,可以使用 ASP.NET 路由時,將 的值 runAllManagedModulesForAllRequests 設定 false 為 或省略。 如需詳細資訊,請參閱 MSDN 網站上的 ASP.NET 路由

預設值是 false
runManagedModulesForWebDavRequests 選擇性布林值。

如果 Managed 模組可以處理 WebDAV 要求,則為 True;否則為false

預設值是 false

子元素

元素 描述
add 選擇性項目。

將模組新增至模組的集合。
clear 選擇性項目。

從 modules 集合中移除模組的所有參考。
remove 選擇性項目。

從 modules 集合中移除模組的參考。

組態範例

此範例會為 IIS 7 整合模式中執行的 Web 應用程式設定模組。

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

範例程式碼

注意

本檔中的範例說明如何使用儲存在 .NET 全域組件快取 (GAC) 的 Managed 程式碼元件。 使用這些範例中的程式碼來部署您自己的元件之前,您需要從 GAC 擷取元件資訊。 若要這樣做,請使用下列步驟:

  • 在 Windows 檔案總管中,開啟您的 C:\Windows\元件路徑,其中 C: 是您的作業系統磁片磁碟機。
  • 找出您的元件。
  • 以滑鼠右鍵按一下元件,然後按一下 [ 屬性]。
  • 複製 文化特性 值;例如: 中性
  • 複製 版本 號碼;例如: 1.0.0.0
  • 複製 公開金鑰權杖 值;例如: 426f62526f636b73
  • 按一下 [取消]

下列程式碼範例會為名為 Contoso 的網站啟用 Managed 模組。 name屬性會定義模組的名稱 CartHeader、type屬性會定義模組的 Managed 類型、preCondition屬性會定義 IIS 只針對 Managed 要求叫用模組。

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()