共用方式為


逐步解說:建立託管 Web 核心的組態檔

本逐步解說示範如何建立組態檔,以搭配 IIS 7 中提供的託管 Web Core 功能使用。

當您在 IIS 7 中建立使用託管 Web Core 功能的應用程式時,您必須提供遵循ApplicationHost.config檔案格式的自訂群組態檔。 根據您的組態設定,您的應用程式可以在您裝載使用 IIS 7 的網站所在的相同伺服器上裝載網頁和應用程式。 例如,如果您的 Web 服務器只有一個網站在 TCP 埠 80 上提供網頁,您可以將應用程式設定為在 TCP 埠 8080 上提供網頁。

因為您的應用程式需要自訂群組態檔,所以您的網站和裝載的 Web Core 應用程式不會共用相同的功能設定。 例如,您可以設定您的網站來實作應用程式不需要的動態功能,或者您可以設定應用程式來要求網站未使用的特定驗證方法。

即使您的 IIS 7 ApplicationHost.config檔案可能包含多個應用程式集區,IIS 7 中的託管 Web Core 功能仍僅支援單一應用程式集區。

必要條件

需要下列軟體才能完成範例中的步驟:

  • Windows Vista 上的 IIS 7。

注意

雖然您必須在 Windows Vista 上執行裝載的 Web Core 應用程式,但您不需要在 Windows Vista 上建立組態檔。 您可以在不同版本的 Windows 上建立組態檔,然後將組態檔複製到已安裝 Windows Vista 的電腦。

  • Visual Studio 2005。

注意

您也可以使用 Visual Studio .NET 2003 或更早版本,雖然逐步解說步驟可能不相同。

建立組態檔

本逐步解說的這個部分的步驟將協助您建立新的組態檔,以搭配裝載的 Web Core 應用程式使用,其中包含提供靜態內容所需的設定。

若要建立組態檔

  1. 啟動 Visual Studio 2005。

  2. 建立新的組態檔:

    1. 在 [ 檔案 ] 功能表上,指向 [ 新增],然後按一下 [ 檔案]。

      [ 新增檔案 ] 對話方塊隨即開啟。

    2. 在 [ 類別] 窗格中,按一下 [ 一般]。

    3. 在 [ 範本] 窗格中,選取 [XML 檔案]。

    4. 按一下 [開啟]

      新的 XML 檔案會以下列 XML 程式碼開啟:

      <?xml version="1.0" encoding="UTF-8"?>  
      
  3. 若要將此 XML 檔案識別為應用程式的組態檔,請在 元素下 <?xml?> 新增下列 XML 程式碼:

    <configuration>  
    
    </configuration>  
    

定義組態區段

建立組態檔時,您必須定義組態檔將包含的組態區段。 您可以藉由將 專案新增 <configSections> 至組態檔來執行此動作。

新增 < configSections 區 > 段

  1. 若要建立區段來定義組態檔將包含哪些區段,請在 元素內 <configuration> 新增下列 XML 程式碼:

    <configSections>  
    
    </configSections>  
    
  2. 若要定義區段中將包含 <system.applicationHost> 哪些資訊,請在 元素內 <configSections> 新增下列 XML 程式碼:

    <sectionGroup name="system.applicationHost"  
            type="System.ApplicationHost.Configuration.SystemApplicationHostSectionGroup">  
        <section name="applicationPools"  
                type="System.ApplicationHost.Configuration.ApplicationPoolsSection"  
                allowDefinition="MachineOnly"  
                overrideModeDefault="Deny" />  
        <section name="listenerAdapters"  
                type="System.ApplicationHost.Configuration.ListenerAdaptersSection"  
                allowDefinition="MachineOnly"  
                overrideModeDefault="Deny" />  
        <section name="log"  
                type="System.ApplicationHost.Configuration.LogSection"  
                allowDefinition="MachineOnly"  
                overrideModeDefault="Deny" />  
        <section name="sites"  
                type="System.ApplicationHost.Configuration.SitesSection"  
                allowDefinition="MachineOnly"  
                overrideModeDefault="Deny" />  
    </sectionGroup>  
    
  3. 若要定義區段中將包含 <system.applicationHost> 哪些資訊,請在 元素內 <configSections> 新增下列程式碼:

    <sectionGroup name="system.webServer"  
        type="System.WebServer.Configuration.SystemWebServerSectionGroup">  
        <section name="defaultDocument"  
            type="System.WebServer.Configuration.DefaultDocumentSection"  
            overrideModeDefault="Allow" />  
        <section name="directoryBrowse"  
            type="System.WebServer.Configuration.DirectoryBrowseSection"  
            overrideModeDefault="Allow" />  
        <section name="globalModules"  
            type="System.WebServer.Configuration.GlobalModulesSection"  
            allowDefinition="MachineOnly"  
            overrideModeDefault="Deny" />  
        <section name="handlers"  
            type="System.WebServer.Configuration.HandlersSection"  
            overrideModeDefault="Deny" />  
        <section name="httpLogging"  
            type="System.WebServer.Configuration.HttpLoggingSection"  
            overrideModeDefault="Deny" />  
        <section name="modules"  
            type="System.WebServer.Configuration.ModulesSection"  
            allowDefinition="MachineToApplication"  
            overrideModeDefault="Deny" />  
        <sectionGroup name="security"  
            type="System.WebServer.Configuration.SecuritySectionGroup">  
            <section name="access"  
                type="System.WebServer.Configuration.AccessSection"  
                overrideModeDefault="Deny" />  
            <sectionGroup name="authentication"  
                type="System.WebServer.Configuration.AuthenticationSectionGroup">  
                <section name="anonymousAuthentication"  
                    type="System.WebServer.Configuration.AnonymousAuthenticationSection"  
                    overrideModeDefault="Allow" />  
            </sectionGroup>  
        </sectionGroup>  
        <section name="staticContent"  
            type="System.WebServer.Configuration.StaticContentSection"  
            overrideModeDefault="Deny" />  
    </sectionGroup>  
    

<新增 system.applicationHost > 設定

當您定義組態檔將包含哪些區段時,您必須使用應用程式所需的設定來填入組態區段。

新增 < system.applicationHost > 區段

  1. 若要將 區 <system.applicationHost> 段新增至組態檔,請在 元素內 <configuration> 新增下列 XML 程式碼:

    <system.applicationHost>  
    
    </system.applicationHost>  
    
  2. 若要建立應用程式集區,請在 元素內 <system.applicationHost> 新增下列 XML 程式碼:

    <applicationPools>  
        <add name="TestAppPool" />  
        <applicationPoolDefaults>  
            <processModel identityType="NetworkService" />  
        </applicationPoolDefaults>  
    </applicationPools>  
    
  3. 若要定義 HTTP 的接聽程式配接器,請在 元素內 <system.applicationHost> 新增下列 XML 程式碼:

    <listenerAdapters>  
        <add name="http" />  
    </listenerAdapters>  
    
  4. 若要建立網站,請在 元素內 <system.applicationHost> 新增下列 XML 程式碼:

    <sites>  
        <site name="Test Web Site" id="1">  
            <application path="/">  
                <virtualDirectory path="/"  
                    physicalPath="D:\inetpub\TestPath\wwwroot" />  
            </application>  
            <bindings>  
                <binding protocol="HTTP" bindingInformation="*:8080:" />  
            </bindings>  
        </site>  
        <siteDefaults>  
            <logFile directory="D:\inetpub\TestPath\Logs" />  
        </siteDefaults>  
        <applicationDefaults applicationPool="TestAppPool" />  
        <virtualDirectoryDefaults allowSubDirConfig="true" />  
    </sites>  
    

注意

您可以視需要變更應用程式之 XML 設定中的檔案路徑和伺服器系結。

<新增 system.webServer > 設定

新增 < system.webServer > 區段

  1. 若要將 區 <system.webServer> 段新增至組態檔,請在 元素內 <configuration> 新增下列 XML 程式碼:

    <system.webServer>  
    
    </system.webServer>  
    
  2. 若要定義您的應用程式將使用的全域模組,請在 元素內 <system.webServer> 新增下列 XML 程式碼:

    <globalModules>  
        <add name="DefaultDocumentModule"  
            image="D:\Windows\system32\inetsrv\defdoc.dll" />  
        <add name="DirectoryListingModule"  
            image="D:\Windows\system32\inetsrv\dirlist.dll" />  
        <add name="StaticFileModule"  
            image="D:\Windows\system32\inetsrv\static.dll" />  
        <add name="AnonymousAuthenticationModule"  
            image="D:\Windows\system32\inetsrv\authanon.dll" />  
        <add name="HttpLoggingModule"  
            image="D:\Windows\system32\inetsrv\loghttp.dll" />  
    </globalModules>  
    
  3. 若要定義應用程式將使用的 <system.webServer> 處理常式,請在 元素內新增下列 XML 程式碼:

    <handlers>  
        <add name="StaticFile" path="*" verb="*"  
            modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"  
            resourceType="Either" requireAccess="Read" />  
    </handlers>  
    
  4. 若要定義應用程式將使用的 <system.webServer> 模組,請在 元素內新增下列 XML 程式碼:

    <modules>  
        <add name="DefaultDocumentModule" />  
        <add name="DirectoryListingModule" />  
        <add name="StaticFileModule" />  
        <add name="AnonymousAuthenticationModule" />  
        <add name="HttpLoggingModule" />  
    </modules>  
    
  5. 若要定義瀏覽目錄和 HTTP 記錄選項,請在 元素內 <system.webServer> 新增下列 XML 程式碼:

    <directoryBrowse enabled="true" />  
    <httpLogging dontLog="false" />  
    
  6. 若要啟用預設檔,請在 元素內 <system.webServer> 新增下列 XML 程式碼:

    <defaultDocument enabled="true">  
        <files>  
            <add value="default.htm" />  
        </files>  
    </defaultDocument>  
    
  7. 若要定義應用程式將實作的 MIME 類型,請在 元素內 <system.webServer> 新增下列 XML 程式碼:

    <staticContent>  
        <mimeMap fileExtension=".gif" mimeType="image/gif" />  
        <mimeMap fileExtension=".htm" mimeType="text/html" />  
        <mimeMap fileExtension=".jpg" mimeType="image/jpeg" />  
        <mimeMap fileExtension=".txt" mimeType="text/plain" />  
    </staticContent>  
    

    注意

    您可以新增更多 <mimeMap> 元素,以針對您的應用程式適當新增 MIME 類型。

  8. 若要定義應用程式的安全性選項,請在 元素內 <system.webServer> 新增下列 XML 程式碼:

    <security>  
        <access flags="Read" sslFlags="None" />  
        <authentication>  
            <anonymousAuthentication enabled="true"  
                userName="IUSR" defaultLogonDomain="" />  
        </authentication>  
    </security>  
    

    當您完成上述所有步驟時,您需要將您的組態檔儲存到裝載的 Web Core 應用程式能夠存取的路徑。

    如果您的組態檔區段未正確定義,您的應用程式可能會遇到錯誤。 視錯誤而定,您可以藉由檢查伺服器的事件檢視器記錄和應用程式自動建立的 IIS 記錄檔,來擷取問題的其他資訊。 如需針對託管 Web Core 應用程式問題進行疑難排解的詳細資訊,請參閱逐步解說 :建立託管的 Web Core 應用程式中所述的疑難排解步驟。

另請參閱

建立託管的 Web Core 應用程式
逐步解說:建立託管的 Web Core 應用程式