Asp.Net webconfig中使用configSections的用法

Asp.Net webconfig中使用configSections的用法

      最近闲来无事,研究研究公司的框架,无心中打开了webconfig页面,发现了一个我不认识的节点<configSections></configSections>,因而百度之,大体的了解了它的做用,仍是蛮重要的!!!可是我竟然不知道!!!这是最骚的,瞬间以为本身仍是太年轻了!!!好了,不BB了,言归正传了。html

一、configSections有什么用node

你们都知道,webconfig文件中不能随意的添加节点,稍有不慎,浏览器就GG了,报错了,玩完了,整我的都很差了,(固然仅限于配置文件,你若是在外部XML文件了定义节点,而后生成对象,那就是你想怎么定义就怎么定义)。web

因此configSections节点就是干这个事的,让你在webconfig中定义自想要定义的节点,固然确定是要按照它指定的规则来!!!下面就来讲configSection制定的规则。sql

 

二、为何须要自定义节点浏览器

说完configSections做用(帮助咱们按照它的规则建立一系列自定义节点),接下来讲所为何须要自定义节点?框架

为了增长应用程序的可移植性,一般网站须要配置一些自定义的节点,例如:文件上传的路径等,再深刻的应用,能够定义工厂方法须要建立的类。ide

 

三、configSections的使用方法sqlserver

<configSections>
    <sectionGroup name="WebSiteConfig">
      <section name="dbProviders" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/>
      <section name="fileUploadPath" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/>
    </sectionGroup>
  </configSections>

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

(2)、而后定义sectionGroup节点,这个配置节至关于全部section配置节的命名空间。网站

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

 

下面开始设置自定义节点

<WebSiteConfig>
    <dbProviders>
      <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>();
            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());
                }
            }
        }
    }
}

相关文章
相关标签/搜索