Asp.net 中创建新的配置节 by zhouwillpower

转自(http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconcreatingnewsectionhandlers.asp)

您可以用自己的 XML 配置标记扩展标准的 ASP.NET 配置设置集。若要完成该操作,您必须创建自己的配置节处理程序。该处理程序必须是一个实现 IConfigurationSectionHandler 接口的 .NET Framework 类。节处理程序解释并处理 Web.config 文件特定部分中 XML 标记中定义的设置并根据配置设置返回适当的配置对象。处理程序类返回的配置对象可以是任何数据结构;它不限于任何基配置类或配置格式。

namespace System.Web.Configuration
{
   public interface IConfigurationSectionHandler
   {
      public Object Create(Object parent, Object input,
         XmlNode node);
   }
}

您还可以定义自己的节,该节与 <appSettings> 节使用相同的配置处理程序。例如:

<configuration>
   <configSections>
      <sectionGroup name="myGroup">
         <sectionGroup name="nestedGroup">
            <section name="mySection" type=
               "System.Configuration.NameValueSectionHandler,System"/>
         </sectionGroup>
      </sectionGroup>
   </configSections>

   <myGroup>
      <nestedGroup>
         <mySection>
            <add key="key_one" value="1"/>
            <add key="key_two" value="2"/>
         </mySection>
      </nestedGroup>
   </myGroup>
</configuration>
NameValueCollection config = (NameValueCollection)
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection");
Response.Write("The value of key_one is " + Server.HtmlEncode(config["key_one"]) + "<br>");
Response.Write("The value of key_two is " + Server.HtmlEncode(config["key_two"]) + "<br>");

posted on 2006-06-21 18:14  zhouwillpower  阅读(153)  评论(0)    收藏  举报