Web.config自定义节点configSections

Web.config自定义节点configSections

所有的自定义节点都在<ConfigSections></ConfigSections>中配置。

为什么需要自定义节点

为了增加应用程序的可移植性和配置方便,通常网站需要配置一些自定义的节点,例如:文件上传的路径、URL重写规则等,再深入的应用,可以定义工厂方法需要创建的类。

 

configSections使用方法

2.1 configSections自定义节点示例

configSections节点下定义自定义节点可以帮我们实现我们自己的节点。

首先定义自己的节点,定义方法如下:

<configSections>

    <sectionGroup name="section group name">

        <section name="section name" type="configuration section handler class" />

    </sectionGroup>

</configSections>

2.2 configSectionssectionGroupsection的关系

sectionGroup 元素充当 section 元素的容器,对section元素进行逻辑分组,以避免命名冲突;当section不是很多时,可以不使用sectionGroup元素;

sectionGroup 元素和 section 元素必须包含在configSections元素中,configSections元素包含在web.config根元素configuration中;configSections元素中可以直接包含sectionGroup元素,也可以直接包含section元素,也可以混合包含

2.3 sectionGroupsection的属性说明

1sectionGroup节点属性

name:必选的 String 属性,指定自定义节点元素的组名称(见下文示例)

2section节点属性

name:必选的 String 属性,指定自定义节点元素的组名称;

type:必选的 String 属性,指定读取并处理自定义节点元素中配置信息的程序类和程序集,程序类必须实现System.Configuration.IConfigurationSectionHandler 接口或 System.Configuration.ConfigurationSection 类的 .NET Framework 类,System.Configuration.IConfigurationSectionHandler接口中,只有一个方法:

//创建配置节处理程序

Object Create (Object parent, Object configContext, XmlNode section)

//返回值:创建的节处理程序对象。

下面举例说明自定义节点的具体用法。

 

自定义节点用法示例

首先,我们新建一个网站项目,并在web.config中加入以下节点:

<configSections>

    <sectionGroup name="WebSiteInfo">

        <section name="basicInfo" type="ConfigurationSectionTest.WebSiteInfoHandler"/>

        <section name="fileUpload" type="ConfigurationSectionTest.WebSiteInfoHandler"/>

    </sectionGroup>

</configSections>

<!-- 以下是自定义节点,自定义节点元素名称即为上面sectionGroup节点和section节点中的name属性值 -->

<WebSiteInfo>

    <basicInfo>

        <add key="name" value="huchen's homepage"/>

        <add key="version" value="1.0"/>

    </basicInfo>

    <fileUpload>

        <add key="fileUploadPath" value="E:\\MyHomePage\\Web\\Upload\\"/>

        <add key="fileUploadSizeMax" value="2M"/>

    </fileUpload>

</WebSiteInfo>

以上我们在WebSiteInfo节点下定义了两个节点basicInfofileUpload,并定义了节点处理程序类 ConfigurationSectionTest.WebSiteInfoHandler,并且随后运用了我们定义的节点。

 

我们来看看节点处理程序ConfigurationSectionTest.WebSiteInfoHandler

任意建立一个项目,新建一个类,或者直接在App_Code里新建一个类,如下:

namespace ConfigurationSectionTest

{

    /// <summary>

    ///WebSiteInfoHandler 的摘要说明

    /// </summary>

    public class WebSiteInfoHandler : IConfigurationSectionHandler

    {

        public WebSiteInfoHandler()

        {

            //

            //TODO: 在此处添加构造函数逻辑

            //

        }

        #region IConfigurationSectionHandler 成员

        public object Create(object parent, object configContext, System.Xml.XmlNode section)

        {

           //这里我们首先返回个hello,并且在此处设置一个断点。看看程序什么时候执行到这。

            return "hello";

        }

        #endregion

    }

}

然后在Default.aspxPage_Load事件处理程序中去访问我们自定义的节点,并在 ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo"); 这条语句上设置断点。

 

protected void Page_Load(object sender, EventArgs e)

{

    Object o = ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo");

}

 

好了,我们启动调试,看到程序首先执行到 ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo");这句。

然后执行到ConfigurationSectionTest.WebSiteInfoHandler中的Create函数。

我们再看看这时处理函数中参数的值:

parentnull

configContext 为配置上下文对象。

section InnerXml<add key="name" value="huchen's homepage" /><add key="version" value="1.0" />

 

F11继续执行return "hello", 继续执行...

在执行到Object o = ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo")后面的“}“,我们发现o的值 ”hello”

 

相信您已经明白的差不多了,当读取自定义节点的内容时,程序去执行我们定义的节点处理程序,并把节点中的内容传给Create函数中的参 数。然后我们在Create中自己处理节点下的内容,并返回我们格式化后的节点内容给调用者,也就是

ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo")

好,知道这些以后,我们就来完善我们的代码,我们在Create中处理传进来的节点内容。

 

为了简单起见,我们引入两个类NameValueCollectionNameValueSectionHandler

NameValueCollection:表示可通过键或索引访问的关联 String 键和 String 值的集合。

NameValueSectionHandler:提供配置节中的名称/值对配置信息。

NameValueSectionHandler 这个类也继承IConfigurationSectionHandler

反编译可以看出NameValueSectionHandler Create方法把参数section的结果转化成了一个集合,就是NameValueCollection

那么我们可以在节点处理程序中的Create函数中写如下代码:

 

NameValueCollection configs;

NameValueSectionHandler baseHandler = new NameValueSectionHandler();

configs =(NameValueCollection)baseHandler.Create(parent,configContext,section);

Return configs;

这样我们就可以这样访问我们的节点了:

 

string myWebSiteName =

((NameValueCollection)ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo"))["name"];

 

Default.aspxPage_Load事件处理程序中添加如下代码:

 

string myWebSiteName =

((NameValueCollection)ConfigurationSettings.GetConfig("WebSiteInfo/basicInfo"))["name"];

Response.Write(myWebSiteName);

 

Ctrl+F5运行,可以看到页面输出了huchen's homepage

 

posted @ 2010-06-05 14:44  弹着钢琴设计  阅读(640)  评论(0编辑  收藏  举报