共用方式為


新增預設檔檔 <>

概觀

<add>集合的 <defaultDocument> 元素會指定要加入元素中 <files> 預設檔案清單的唯一檔案名。

相容性

版本 備註
IIS 10.0 <add> IIS 10.0 中未修改專案。
IIS 8.5 <add> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <add>
IIS 7.5 <add> 在 IIS 7.5 中修改專案。
IIS 7.0 <add>集合的 <defaultDocument> 元素是在 IIS 7.0 中引進的。
IIS 6.0 <defaultDocument>集合會取代IIsWebService中繼基底物件上DirBrowseFlags屬性的EnableDefaultDoc 屬性和 EnableDefaultDoc值。

安裝程式

<add>集合的 <defaultDocument> 元素包含在 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. 在 [ 新增預設檔 ] 對話方塊中,輸入您要在 [ 名稱 ] 方塊中新增的預設檔案名稱,然後按一下 [ 確定]。
    [新增預設檔] 對話方塊的螢幕擷取畫面。在 [名稱] 方塊中,會寫入主點 h t m l。

  6. 如有必要,在 [ 動作 ] 窗格中,選取清單中的預設檔,然後按一下 [ 上移 ] 或 [ 下移 ] 來定義 IIS 應該搜尋預設檔案清單的順序。

  7. 在 [ 預設檔 警示] 方塊中,按一下 [ ] 以拒絕父組態層級的組態繼承,或按一下 [ ] 或 [ 取消] 取消 預設檔順序中的變更。
    [預設檔] 對話方塊的螢幕擷取畫面。

  8. 如有需要,請按一下 [動作] 窗格中的 [移除],移除您不想要作為預設檔使用的任何檔案名。

組態

屬性

屬性 描述
name 選擇性字串屬性。

指定可用來做為預設檔的 Web 內容的檔案名。 值在檔案集合中必須是唯一的,而且可以是檔案名或相對路徑。

子元素

無。

組態範例

下列組態範例會在月臺或應用程式的Web.config檔案中包含時,啟用網站或應用程式的預設檔。 然後,它會將檔案名 「Home.html」 新增至網站或應用程式的預設檔案清單。

<configuration>
   <system.webServer>
      <defaultDocument enabled="true">
         <files>
            <add value="home.html" />
         </files>
      </defaultDocument>
   </system.webServer>
</configuration>

範例程式碼

下列範例會在名為 Contoso 的網站上啟用預設檔,然後將名為 Home.html 的檔案新增至網站的預設檔案清單。

AppCmd.exe

appcmd.exe set config "Contoso" /section:defaultDocument /enabled:true /+files.[value='home.html']

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 defaultDocumentSection = config.GetSection("system.webServer/defaultDocument");
            
            defaultDocumentSection["enabled"] = true;
            
            ConfigurationElementCollection filesCollection = defaultDocumentSection.GetCollection("files");
            ConfigurationElement addElement = filesCollection.CreateElement("add");
            addElement["value"] = @"home.html";
            filesCollection.AddAt(0, addElement);
            
            serverManager.CommitChanges();
        }
    }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Class Sample
   Shared Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Contoso")
      Dim defaultDocumentSection As ConfigurationSection = config.GetSection("system.webServer/defaultDocument")

      defaultDocumentSection("enabled") = True

      Dim filesCollection As ConfigurationElementCollection = defaultDocumentSection.GetCollection("files")
      Dim addElement As ConfigurationElement = filesCollection.CreateElement("add")
      addElement("value") = "home.html"
      filesCollection.AddAt(0, addElement)

      serverManager.CommitChanges()
   End Sub
End Class

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso";

var defaultDocumentSection = adminManager.GetAdminSection("system.webServer/defaultDocument",
   "MACHINE/WEBROOT/APPHOST/Contoso");

defaultDocumentSection.Properties.Item("enabled").Value = true;

var filesCollection = defaultDocumentSection.ChildElements.Item("files").Collection;

var addElement = filesCollection.CreateNewElement("add");
addElement.Properties.Item("value").Value = "home.html";
filesCollection.AddElement(addElement, 0);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso"

Set defaultDocumentSection = adminManager.GetAdminSection("system.webServer/defaultDocument", _
   "MACHINE/WEBROOT/APPHOST/Contoso")

defaultDocumentSection.Properties.Item("enabled").Value = True  

Set filesCollection = defaultDocumentSection.ChildElements.Item("files").Collection

Set addElement = filesCollection.CreateNewElement("add")
addElement.Properties.Item("value").Value = "home.html"
filesCollection.AddElement addElement, 0

adminManager.CommitChanges