ASP.NET Lab

The Best Web, The Best Future

博客园 首页 新随笔 订阅 管理

你可以使用配置元素对 ASP.NET 配置设定的标准集进行扩展。要这样做,你必须先创建属于自己的自定义配置段处理器。

配置段处理器必须是实现了 System.Configuration.ConfigurationSection 的 .NET Framework 类。

提示:在 .NET Framework 1.0 和 1.1 中,配置段处理器实现了 System.Configuration.IConfigurationSectionHandler 接口,现在已经不赞成继续使用。

配置段处理器对 Web.config 文件特定位置的 XML 配置元素设定进行说明和处理,并且返回合适的配置对象,该对象基于配置设定。配置段处理器类返回的配置对象可以是任何一种数据结构;并没有对配置基类或配置格式进行限制。ASP.NET 使用配置对象对自定义配置元素进行读取和写入。

创建自定义配置段处理器

  1. 创建一个继承于 System.Configuration.ConfigurationSection 的公共类,如下例代码所示。

    using System;
    using System.Collections;
    using System.Text;
    using System.Configuration;
    using System.Xml;
    
    namespace MyConfigSectionHandler
    {
        public class MyHandler : ConfigurationSection
        {
            public MyHandler()
            {
            }
    
            // 按照如下方式添加子元素和参数的声明:
            // [ConfigurationProperty("<propertyName>", <named parameters>)]
            // public <type> <PropertyName>
            // {
            //     get { return (<type>)this["<propertyName>"]; }
            //     set { this["<propertyName>"] = value; }
            // }
        }
    }
    
  2. 添加你自己的代码来完成所需要完成的配置操作。

    例如,你可以把被注释的代码替换成下例代码,以便从自定义配置段中获取相应的设定值。

    using System;
    using System.Collections;
    using System.Text;
    using System.Configuration;
    using System.Xml;
    
    namespace MyConfigSectionHandler
    {
        public class MyHandler : ConfigurationSection
        {
            public MyHandler()
            {
            }
    
            public MyHandler(String attribVal)
            {
                MyAttrib1 = attribVal;
            }
    
            [ConfigurationProperty("myAttrib1", DefaultValue = "Clowns", IsRequired = true)]
            [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
            public String MyAttrib1
            {
                get
                { return (String)this["myAttrib1"]; }
                set
                { this["myAttrib1"] = value; }
            }
    
            [ConfigurationProperty("myChildSection")]
            public MyChildConfigElement MyChildSection
            {
                get
                { return (MyChildConfigElement)this["myChildSection"]; }
                set
                { this["myChildSection"] = value; }
            }
        }
    
        public class MyChildConfigElement : ConfigurationElement
        {
            public MyChildConfigElement()
            {
            }
    
            public MyChildConfigElement(String a1, String a2)
            {
                MyChildAttribute1 = a1;
                MyChildAttribute2 = a2;
            }
    
            [ConfigurationProperty("myChildAttrib1", DefaultValue = "Zippy", IsRequired = true)]
            [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
            public String MyChildAttribute1
            {
                get
                { return (String)this["myChildAttrib1"]; }
                set
                { this["myChildAttrib1"] = value; }
            }
    
            [ConfigurationProperty("myChildAttrib2", DefaultValue = "Michael Zawondy", IsRequired = true)]
            [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
            public String MyChildAttribute2
            {
                get
                { return (String)this["myChildAttrib2"]; }
                set
                { this["myChildAttrib2"] = value; }
            }
        }
    }
    

    该实例使用的是声明模型。当然,System.Configuration.ConfigurationSection 类也可以使用编程模型实现。

    相比之下,该实例与“ASP.NET 实践:使用 IConfigurationSectionHandler 创建自定义配置段”中的代码实例比较类似。但是,对 System.Configuration.ConfigurationSection 进行继承能够让你对配置段处理器进行更好的控制。例如,在下一个步骤中的配置文件允许将上述代码中声明了 ConfigurationProperty 的子元素 myChildSection 定义成继承自 ConfigurationElement 的类。另外,ConfigurationElementCollection 类中对集合的功能性封装还允许你更加容易地创建集合元素,并使用配置文件中的 addremove、以及 clear 元素。

在 ASP.NET 配置文件中添加自定义配置段处理器

  1. 添加 sectionGroup 元素和 section 元素到 Web.config 文件中的 configSections 元素下,如下例代码所示。自定义配置段处理器的声明与配置段的名称相关联。

    提示:sectionGroup 中被嵌套的配置段元素是可选的,因为它不利于对配置数据进行良好的组织,所以不推荐使用。

    在添加自定义配置元素的时候,你可以把配置段处理器的声明添加到不同的配置文件中,并且提供配置文件层次中更高级别的配置段处理器定义。

    section 元素的 type 参数必须与汇编集清单相匹配,不然会产生配置错误。汇编集文件自身也必须与定义它的 Web.config 文件位于相同的 ASP.NET 应用程序目录中。

    <configuration>
    
    <!-- 配置段处理器的声明区。 -->
      <configSections>
        <sectionGroup name="myCustomGroup">
          <section 
            name="myCustomSection" 
            type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
            allowLocation="true" 
            allowDefinition="Everywhere"
          />
        </sectionGroup>
          <!-- 其他的 <section> 和 <sectionGroup> 元素。 -->
      </configSections>
    
      <!-- 配置段的设定区。 -->
    
    </configuration>
    
  2. 添加自定义配置元素到 Web.config 文件的配置段设定区。

    <configuration>
    
    <!-- 配置段处理器声明区。 -->
    
      <!-- 配置段的设定区。 -->
      <myCustomGroup>
        <myCustomSection myAttrib1="Clowns">
          <myChildSection 
              myChildAttrib1="Zippy" 
              myChildAttrib2="Michael Zawondy "/>
        </myCustomSection>
      </myCustomGroup>
    
      <!-- 其他配置的设定,例如 <system.web> -->
    
    </configuration>
    

编程访问自定义配置数据

  • 获取一个自定义配置对象的实例并使用 System.Configuration.ConfigurationManager.GetSection 方法或 System.Web.Configuration.WebConfigurationManager.GetSection 方法来进行操作。

    下例 ASPX 页面实例与前面的实例一起工作,并列举出自定义配置段中的所有参数集和子元素集。

    <%@ Page Language="C#" %>
    
    <script runat="server">
        protected void Button1_Click(object sender, EventArgs e)
        {
            MyConfigSectionHandler.MyHandler config =
                (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection(
                "myCustomGroup/myCustomSection");
            
            StringBuilder sb = new StringBuilder();
    
            sb.Append("<h2>myCustomSection 元素中的参数集:</h2>");
            sb.AppendFormat("myAttrib1 = {0}<br/>", config.MyAttrib1.ToString());
            sb.Append("<h2>Attributes in the myChildSection Element:</h2>");
            sb.AppendFormat("myChildAttrib1 = {0}<br/>", config.MyChildSection.MyChildAttribute1.ToString());
            sb.AppendFormat("myChildAttrib2 = {0}<br/>", config.MyChildSection.MyChildAttribute2.ToString());
    
            Label1.Text = sb.ToString();
            Label1.Visible = true;
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页面</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        <h1>列举 MyCustomSection</h1>
        <asp:Label ID="Label1" runat="server" 
            Text="" />
        <br />
        <asp:Button ID="Button1" runat="server" 
            Text="获取自定义配置信息" 
            OnClick="Button1_Click" />
    
        </div>
        </form>
    </body>
    </html>
    
posted on 2006-12-23 11:13  Laeb  阅读(1533)  评论(0编辑  收藏  举报