Web.config custom node configSections

1. Why do we need custom node

In order to increase application portability, the site usually need to configure some custom nodes, such as: path such as file upload, and then in-depth application, you can define a class factory method need to create.

2.configSections use

Definition configSections node custom node can help us achieve our own node.

First, define your own node, is defined as follows:

<configSections>

    <sectionGroup name="section group name">

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

    </sectionGroup>

</configSections>

 

 Define your own node must configSections node.

sectionGroup element acts as a container section elements.

The section element configuration section handler associated with the configuration element or section. Because ASP.NET does not deal with how to set up in the configuration file to make any assumptions, so it is very necessary. However, ASP.NET will delegate the processing configuration data to the configuration section of the handler (wait description.) Each section identifies each element or section of a configuration element. It can be logically grouped in sectionGroup element section element, to organize the section element and to avoid naming conflicts. section and sectionGroup configSections element contained in the element.

sectionGroup node attributes:

name: String mandatory attribute, which is the name of the section elements in the group setting area configuration file.

section node attributes:

name: String mandatory attribute, the type attribute specifies the name specified in the configuration section configuration section or element associated handler. This is the name of the element used in the section settings area of ​​the configuration file.

type: String mandatory attribute that specifies the name of the configuration for performing the processing section to the class of the following operations: processing configuration settings specified in the name attribute sections or elements.

 

Well now define your own node, the node can use. Use as follows:

<section group name>

    <section name>

        <add key="key1" value="value1" />

    </section name>

</section group name> 

 

 Defined the own node, node information on how to read it?

The following are the exact words on msdn:

You can use your own XML configuration elements to extend the standard set of ASP.NET configuration settings. To do this, you must create your own configuration section handler.

The handler must be a .NET Framework class System.Configuration.IConfigurationSectionHandler System.Configuration.ConfigurationSection interfaces or class implementation.

Interpreting and processing section handlers particular portion of the XML file Web.config configuration setting elements defined in the configuration object and returns the appropriate according to the configuration settings. The return handler class configuration object may be any data structure; it is not limited to any base configuration class configuration or format. Use the ASP.NET configuration object to perform reading and writing custom configuration elements.

That is the meaning of this passage above, we define a class that inherits from System.Configuration.IConfigurationSectionHandler to System.Configuration.ConfigurationSection interface or class.

Then use this class to handle our custom node.

我们看到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>

<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节点下定义了两个节点basicInfo和fileUpload,并定义了节点处理程序类ConfigurationSectionTest.WebSiteInfoHandler,并且随后运用了我们定义的节点。

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

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

并在Create函数中设置一个断点。

 

 

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.aspx的Page_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函数。

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

parent为null

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中处理传进来的节点内容。 

为了简单起见,我们引入两个类NameValueCollection,NameValueSectionHandler。

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.aspx的Page_Load事件处理程序中添加如下代码:

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

Response.Write(myWebSiteName);

 

 

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

转载于:https://www.cnblogs.com/zhangchenliang/p/4155314.html

Guess you like

Origin blog.csdn.net/weixin_34291004/article/details/93494844