共用方式為


新增 Modules <add> 元素

概觀

元素 <add><modules> 元素會將模組新增至使用者連接到網站或應用程式時,IIS 管理員中可用的功能模組集合。

注意

此模組集合專屬於 IIS 管理員,不應與 <system.webServer/modules> 集合混淆,這會定義影響 HTTP 要求處理的模組。

注意

元素中的<modules>設定只能在 管理員 istration.config 檔案中設定。

相容性

版本 備註
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 元素 <add><modules> 元素是在 IIS 7.0 中引進的。
IIS 6.0 N/A

設定

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

作法

沒有將模組新增至 <modules> IIS 7 元素的使用者介面。 如需如何以程式設計方式將模組新增至 <modules> 專案的範例,請參閱 本檔的<程式代碼範例 >一節。

組態

屬性

屬性 描述
name 必要的字串屬性。

指定 Web 伺服器上 Managed 模組的唯一名稱。

子元素

無。

組態範例

下列範例組態摘錄自 管理員 istration.config 檔案,會將名為 ContosoProvider 的 Managed 模組提供者新增至集合結尾<modules>

注意

此範例組態摘錄已縮短,方便閱讀。

<location path=".">
   <modules>
      <add name="Modules" />
      <add name="Handlers" />

      . . .
      . . .
      . . .

      <add name="ContosoProvider" />
   </modules>
</location>

下列範例組態摘錄自 管理員 istration.config 檔案,藉由定義清除預設模組清單的唯<location>一元素,並定義每個位置分別啟用的特定模組,來指定預設網站和子應用程式的自定義<modules>元素。 當遠端管理連線到任一位置時,IIS 管理員只會顯示每個元素中 <location> 所列模組所代表的功能。

<location path="Default Web Site">
   <modules>
      <clear />
      <add name="DefaultDocument" />
      <add name="DirectoryBrowse" />
   </modules>
</location>

<location path="Default Web Site/ContosoApplication">
   <modules>
      <clear />
      <add name="DefaultDocument" />
      <add name="DirectoryBrowse" />
      <add name="Handlers" />
   </modules>
</location>

範例程式碼

下列程式代碼範例會啟用名為 ContosoProvider 的 Managed 模組提供者至 管理員 istration.config 檔案中的全域位置層級。

AppCmd.exe

注意

您無法使用 AppCmd.exe 來設定 <modules> 設定。

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.GetAdministrationConfiguration();
         ConfigurationSection modulesSection = config.GetSection("modules");

         ConfigurationElementCollection modulesCollection = modulesSection.GetCollection();
         ConfigurationElement addElement = modulesCollection.CreateElement("add");
         addElement["name"] = @"ContosoProvider";
         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.GetAdministrationConfiguration
      Dim modulesSection As ConfigurationSection = config.GetSection("modules")

      Dim modulesCollection As ConfigurationElementCollection = modulesSection.GetCollection
      Dim addElement As ConfigurationElement = modulesCollection.CreateElement("add")
      addElement("name") = "ContosoProvider"
      modulesCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject("Microsoft.ApplicationHost.WritableAdminManager"); 
adminManager.CommitPath = "MACHINE/WEBROOT"; 
adminManager.SetMetadata("pathMapper", "AdministrationConfig");
var modulesSection = adminManager.GetAdminSection("modules", "MACHINE/WEBROOT"); 

var modulesCollection = modulesSection.Collection;
var addElement = modulesCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "ContosoProvider";
modulesCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT"
adminManager.SetMetadata "pathMapper", "AdministrationConfig"
Set modulesSection = adminManager.GetAdminSection("modules", "MACHINE/WEBROOT")

Set modulesCollection = modulesSection.Collection
Set addElement = modulesCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "ContosoProvider"
modulesCollection.AddElement(addElement)

adminManager.CommitChanges()