Asp.Net webconfig中使用configSections的用法

最近闲来无事,研究研究公司的框架,无意中打开了webconfig页面,发现了一个我不认识的节点<configSections></configSections>,于是百度之,大致的了解了它的作用,还是蛮重要的!!!但是我居然不知道!!!这是最骚的,瞬间觉得自己还是太年轻了!!!好了,不BB了,言归正传了。

1、configSections有什么用

大家都知道,webconfig文件中不能随意的添加节点,稍有不慎,浏览器就GG了,报错了,玩完了,整个人都不好了,(当然仅限于配置文件,你如果在外部XML文件了定义节点,然后生成对象,那就是你想怎么定义就怎么定义)。

所以configSections节点就是干这个事的,让你在webconfig中定义自想要定义的节点,当然肯定是要按照它指定的规则来!!!下面就来说configSection制定的规则。

2、为什么需要自定义节点

说完configSections作用(帮助我们按照它的规则创建一系列自定义节点),接下来说所为什么需要自定义节点?

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

3、configSections的使用方法

复制代码
<configSections>
    <sectionGroup name="WebSiteConfig">
      <section name="dbProviders" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/>  //ZC.DBUtility就是后面定义WebSiteInfoHandler处理类的
命名空间
<section name="fileUploadPath" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/> </sectionGroup> </configSections>
复制代码

(1)、定义一个configSection配置节

(2)、然后定义sectionGroup节点,这个配置节相当于所有section配置节的命名空间。

(3)、最后定义section配置节,通过这个配置节设置自定义节点的名称和处理configSection的一般处理程序   注意:这个处理程序必须继承IConfigurationSectionHandler不要问我为什么,你知道为什么!!!

下面开始设置自定义节点

复制代码
<WebSiteConfig>  //WebSiteConfig就是上面sectionGroup的name
    <dbProviders>//上面每个section的名字
      <add key="DBInstance" value="ConnString" type="sqlserver"/>
    </dbProviders>
    <fileUploadPath>
      <add key="path" value="#" />
      <add key="path1" value="#1" />
    </fileUploadPath>
  </WebSiteConfig>
复制代码

自定义节点的定义规则要和上面的configSections的定义规则保持一致

最后编写一般处理程序,按照一定的规则获取我们的自定义节点的信息

复制代码
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Xml;

namespace ZC.DBUtility
{
    public class WebSiteInfoHandler : IConfigurationSectionHandler  //处理程序必须派生与这个接口
    {
        /// <summary>
        /// 返回自定义节点对象字典
        /// </summary>
        /// <param name="parent">父对象</param>
        /// <param name="configContext">配置上下文对象</param>
        /// <param name="section">节 XML 节点</param>
        /// <returns> 创建的节处理程序对象。</returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            Dictionary<string, ConfigEntity> config = new Dictionary<string, ConfigEntity>();  //ConfigEntity就是自定义的数据存储类
foreach (XmlNode node in section) { string key = string.Empty, value = string.Empty, type = string.Empty; if (node.Attributes["key"] != null) key = node.Attributes["key"].Value; if(node.Attributes["value"] != null) value = node.Attributes["value"].Value; if (node.Attributes["type"] != null) type = node.Attributes["type"].Value; config.Add(key, new ConfigEntity(value, type)); } return config; } } }
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZC.DBUtility
{
    public class ConfigEntity
    {
        /// <summary>
        /// 自定义节点对象
        /// </summary>
        /// <param name="_value">节点的value值</param>
        /// <param name="_type">节点的type值</param>
        public ConfigEntity(string _value, string _type) 
        {
            this.Value = _value;
            this.Type = _type;
        }

        public string _value;
        public string _type;
        public string Value {
            get { return _value; }
            set { _value = value; }
        }
        public string Type {
            get { return _type; }
            set { _type = value; }
        }
    }
}
复制代码

ok,做完这几步我们可以开始愉快的使用自定义节点的信息了

复制代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZC.DBUtility;

namespace Web.Ado
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, ConfigEntity> config = ConfigurationSettings.GetConfig("WebSiteConfig/dbProviders") as Dictionary<string, ConfigEntity>;
            if (config != null) {
                foreach (string key in config.Keys) {
                    ConfigEntity ce = config[key] as ConfigEntity;
                    Response.Write(ce.RetrieveFullName());
                }
            }
        }
    }
}
复制代码

原文地址:https://www.cnblogs.com/sjqq/p/7528604.html