共用方式為


預設檔檔檔案 <>

概觀

集合 <files><defaultDocument> 元素會指定設定為預設檔的檔案名清單。 元素 <files> 可以包含專案清單,其中清單中的 <add> 每個專案會指定要加入至 <files> 清單的唯一檔案。

相容性

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

安裝程式

<files>集合的 <defaultDocument> 元素包含在 IIS 7 的預設安裝中。

作法

如何新增應用程式或網站的預設檔

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

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

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

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

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

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager
  2. 在 [ 連線] 窗格中,展開伺服器名稱、展開 [網站],然後流覽至您要設定預設檔的網站或應用程式。

  3. 在 [ 首頁] 窗格中,按兩下 [預設檔]。
    在 [預設網站首頁] 窗格中選取 [預設檔] 的螢幕擷取畫面。

  4. 在 [ 動作 ] 窗格中,按一下 [ 新增...]

  5. 在 [ 新增預設檔 ] 對話方塊中,輸入您要在 [ 名稱 ] 方塊中新增的預設檔案名稱,然後按一下 [ 確定]。
    新增名為 home dot H T M L 之新預設檔的螢幕擷取畫面。

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

  7. 在 [ 預設檔 ] 警示方塊中,按一下 [ ] 以拒絕父設定層級的設定繼承,或按一下 [ ] 或 [ 取消 ] 以取消預設檔順序中的變更。
    在繼承新預設檔的父層級設定之前,警告您。

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

組態

屬性

無。

子元素

元素 描述
add 選擇性項目。

將檔案名新增至檔案的集合。
remove 選擇性項目。

從檔案集合中移除檔案名的參考。
clear 選擇性項目。

從檔案集合中移除檔案名的所有參考。

組態範例

下列組態範例會在月臺或應用程式的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