演练:为托管 Web 核心创建配置文件

本演练演示如何创建配置文件,以便与 IIS 7 中提供的托管 Web 核心功能配合使用。

在 IIS 7 中创建使用托管 Web Core 功能的应用程序时,必须提供遵循 ApplicationHost.config 文件格式的自定义配置文件。 根据配置设置,应用程序可以在托管使用 IIS 7 的网站所在的同一服务器上托管网页和应用程序。 例如,如果 Web 服务器只有一个网站在 TCP 端口 80 上为网页提供服务,则可以将应用程序配置为在 TCP 端口 8080 上为网页提供服务。

由于应用程序需要自定义配置文件,因此网站和托管的 Web Core 应用程序不会共享相同的功能设置。 例如,可以将网站配置为实现应用程序不需要的动态功能,也可以将应用程序配置为要求网站不使用的特定身份验证方法。

尽管 IIS 7 的ApplicationHost.config文件可能包含多个应用程序池,但 IIS 7 中的托管 Web 核心功能仅支持单个应用程序池。

先决条件

需要以下软件才能完成示例中的步骤:

  • 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 核心应用程序
演练:创建托管的 Web Core 应用程序