现在的位置: 首页 > 综合 > 正文

配置文件web.config(一)

2012年12月09日 ⁄ 综合 ⁄ 共 5062字 ⁄ 字号 评论关闭
     
      下面是我学习asp.net中的一点心得,记下了我对每个知识点的理解,如有不正确的地方敬请指正!
      ASP.NET 配置数据存储在 XML 文本文件中,每一个 XML 文本文件都命名为 Web.config。Web.config 文件可以出现在 ASP.NET
应用程序的多个目录中。
      所有的 ASP.NET 配置信息都驻留在 Web.config 文件中的 元素中。此元素中的配置信息分为两个主区域:配置节处理程序声明区域和配置节设置区域。

配置节处理程序声明区域     

      配置节处理程序声明区域驻留在 Web.config 文件中的 元素内。它包含在其中声明节处理程序的 ASP.NET 配置 元素。可以将这些配置节处理程序声明嵌套在 元素中,以帮助组织配置信息。通常,sectionGroup 元素表示要应用配置设置的命名空间。例如,所有的 ASP.NET 配置节处理程序都在

节组中进行分组,如下面的代码示例所示。

    <?xml version="1.0"  encoding="utf-8"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
   

    <configSections><!--自定义的配置节处理程序声明一般放在configSections中-->

    <section name="test" type=".........."/>

    <sectionGroup name="test1"
type="System.Web.Configuration.SystemWebSectionGroup,  System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
         <!-- <section /> elements. -->

    </sectionGroup>
    </configSections>

    <appSettings></appSettings>
   <connectionStrings configSource=""></connectionStrings>
   <System.web><!--这里是系统配置节的声明,如授权认证等--></System.web>
   <!--自定义配置节设置-->
   <test></test>
   <test1>
   <section1></section1>
   <section2></section2>
   </test1>
   </configuration>

    注意上面的结构:首先是configurations配置节,我的理解是它包含的是自定义的配置节和从Machine.config中添加的配置节,自定义的配置节要声明处理函数,包括section和sectionGroup两个节点。然后是应用程序声明appSetting和connectionStrings数据库连接配置节。再后来就是应用程序系统有关的配置节处理程序声明System.web,里面包含了应用程序认证授权等配置节。最后就是对configurations节中的所有配置节进行设置。
    配置节处理程序声明一般格式:
    <section name="loggingSettings" type="AtOneWeb.Common.Configuration.LoggingSettingsSection,AtOneWeb.Common"/>
   配置节设置一般格式:
<loggingFormatters>
                <add name="Text Formatter" type="AtOneWeb.Logging.TextFormatter, AtOneWeb.Logging" template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Machine: {machine}
Application Domain: {appDomain}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"/>
            </loggingFormatters>

比价复杂的设置是:
    <loggingSettings>
            <loggingCategoriesSource>
                <add name="General" switchValue="All">
                    <listeners>
                        <add name="Event Log Destination"/>
                    </listeners>
                </add>
                <add name="Data Access Events" switchValue="All">
                    <listeners>
                        <add name="File Destination"/>
                    </listeners>
                </add>
            </loggingCategoriesSource>
            <loggingListeners>
                <add name="File Destination" type="AtOneWeb.Logging.FileTraceListener, AtOneWeb.Logging" logFormatter="Text Formatter" logFileName="logfile.txt"/>
                <add name="Event Log Destination" type="AtOneWeb.Logging.EventsTraceListener, AtOneWeb.Logging" source="Yimodo" logFormatter="Text Formatter" logMachineName="."/>
            </loggingListeners>
            <loggingFormatters>
                <add name="Text Formatter" type="AtOneWeb.Logging.TextFormatter, AtOneWeb.Logging" template="Timestamp: {timestamp}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Machine: {machine}
Application Domain: {appDomain}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"/>
            </loggingFormatters>
        </loggingSettings>

     配置节设置区域中的每个配置节都有一个节处理程序声明。节处理程序是用来实现 接口的 .NET Framework 类。节处理程序声明中包含配置设置节的名称(如 )以及用来处理该节中配置数据的节处理程序类的名称(如

)。下面的代码示例中阐释了这一点。

      <section name="pages"
type="System.Web.Configuration.PagesSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
</section>

您只需要声明配置节处理程序一次。默认 ASP.NET 配置节的节处理程序已在默认的 Machine.config 文件中进行声明。根 Web.config
文件和 ASP.NET 应用程序中的其他配置文件都自动继承在 Machine.config
文件中声明的配置处理程序。只有当您创建用来处理自定义设置节的自定义节处理程序类时,才需要声明新的节处理程序。

中列出了预定义的 ASP.NET
配置设置节。

配置节设置

配置节设置区域位于配置节处理程序声明区域之后,它包含实际的配置设置。

默认情况下,在内部或者在某个根配置文件中,对于 configSections 区域中的每一个 section
sectionGroup 元素,都会有一个指定的配置节元素。可以在
systemroot\Microsoft.NET\Framework\versionNumber\CONFIG\Machine.config.comments
文件中查看这些默认设置。

配置节元素还可以包含子元素,这些子元素与其父元素由同一个节处理程序处理。例如,下面的 元素包含一个

元素,该元素没有相应的节处理程序,因为它由 pages 节处理程序来处理。

<pages
buffer="true"
enableSessionState="true"
asyncTimeout="45"
<!-- Other attributes. -->
>
<namespaces>
<add namespace="System" />
<add namespace="System.Collections" />
</namespaces>
</pages>

Web.config 文件中的示例   //摘自msdn

下面的代码示例演示上面的代码示例可适合于 Web.config 文件的哪个位置。请注意,pages 元素的
namespaces 元素没有配置节处理程序声明。这是由于
System.Web.Configuration.PagesSection 节处理程序处理 pages 设置节的所有子元素

<?xml version="1.0" encoding="us-ascii"?>
<configuration>

<!-- Configuration section-handler declaration area. -->
  <configSections>
    <sectionGroup name="system.web"
      type="System.Web.Configuration.SystemWebSectionGroup, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <section
        name="pages"
        type="System.Web.Configuration.PagesSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
      />
      <!-- Other <section /> elements. -->
    </sectionGroup>
    <!-- Other <sectionGroup /> and <section /> elements. -->
  </configSections>

<!-- Configuration section settings area. -->
  <pages
    buffer="true"
    enableSessionState="true"
    asyncTimeout="45"
  <!-- Other attributes. -->
  >
    <namespaces>
      <add namespace="System" />
      <add namespace="System.Collections" />
    </namespaces>
  </pages>
  <!-- Other section settings elements. -->

</configuration>

抱歉!评论已关闭.