博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net webconfig中使用configSections的用法
阅读量:4564 次
发布时间:2019-06-08

本文共 3530 字,大约阅读时间需要 11 分钟。

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

1、configSections有什么用

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

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

 

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

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

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

 

3、configSections的使用方法

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

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

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

 

下面开始设置自定义节点

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

 

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

using System;using System.Configuration;using System.Collections.Generic;using System.Xml;namespace ZC.DBUtility{    public class WebSiteInfoHandler : IConfigurationSectionHandler    {        ///         /// 返回自定义节点对象字典        ///         /// 父对象        /// 配置上下文对象        /// 节 XML 节点        /// 
创建的节处理程序对象。
public object Create(object parent, object configContext, System.Xml.XmlNode section) { Dictionary
config = new Dictionary
(); 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    {        ///         /// 自定义节点对象        ///         /// 节点的value值        /// 节点的type值        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
config = ConfigurationSettings.GetConfig("WebSiteConfig/dbProviders") as Dictionary
; if (config != null) { foreach (string key in config.Keys) { ConfigEntity ce = config[key] as ConfigEntity; Response.Write(ce.RetrieveFullName()); } } } }}

 ok,done

 

转载于:https://www.cnblogs.com/GreenLeaves/p/6520472.html

你可能感兴趣的文章
UWP简单示例(三):快速开发2D游戏引擎
查看>>
IEnumerable<T> 接口和GetEnumerator 详解
查看>>
Vue:基本用法。
查看>>
SQLite数据操作
查看>>
Leetc解题笔记-Majority Element
查看>>
【Office_Word】Word排版
查看>>
【Java_多线程并发编程】基础篇——线程状态扭转函数
查看>>
【前端_js】web项目调试
查看>>
mac OS在“安全与隐私”中找不到 “任何来源”选项
查看>>
Autodesk的照片建模云服务—Autodesk ReCap 360 photo 的测试数据
查看>>
bzoj3687 简单题
查看>>
STL容器简介
查看>>
HashMap遍历的两种方式,推荐使用entrySet()
查看>>
如何在Android开发中测试应用在真机上实验
查看>>
传奇代码研究 极品机率...
查看>>
mysql存储过程和定时调用
查看>>
(转)park1.0.0生态圈一览
查看>>
需要学习双拼了
查看>>
查询sql表的详细信息
查看>>
php 在数组中查找某个值
查看>>