最近闲来无事,研究研究公司的框架,无意中打开了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"/>
<section name="fileUploadPath" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/>
</sectionGroup>
</configSections>

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

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

ok,done

Asp.Net webconfig中使用configSections的用法的更多相关文章

  1. ASP.NET Core中Middleware的使用

    https://www.cnblogs.com/shenba/p/6361311.html   ASP.NET 5中Middleware的基本用法 在ASP.NET 5里面引入了OWIN的概念,大致意 ...

  2. Asp.Net MVC中DropDownListFor的用法(转)

    2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T  List<T>的第一个 ...

  3. Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  4. 转:Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  5. ASP.NET MVC中Area的另一种用法

    ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...

  6. ASP.NET中application对象的用法(面试题)

    ASP.NET中application对象的用法 本文导读:Application对象是HttpApplicationState类的一个实例,Application状态是整个应用程序全局的.Appli ...

  7. Asp.Net Core中服务的生命周期选项区别和用法

    在做一个小的Demo中,在一个界面上两次调用视图组件,并且在视图组件中都调用了数据库查询,结果发现,一直报错,将两个视图组件的调用分离,单独进行,却又是正常的,寻找一番,发现是配置依赖注入服务时,对于 ...

  8. 运行ABP(asp.net core 3.X+Vue)提示'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT 的用法无效。

    创建ASP.NET Boilerplate,还原数据库和启动客户端 这里就略过,具体参考 ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) ASP.NET Boilerpl ...

  9. asp.net中Repeater控件用法笔记

    大家可能都对datagrid比较熟悉,但是如果在数据量大的时候,我们就得考虑使用 repeater作为我们的数据绑定控件了.Repeater控件与DataGrid (以及DataList)控件的主要区 ...

随机推荐

  1. Getting Started with Node.js on Heroku

    NodeJS应用托管平台 https://devcenter.heroku.com/articles/getting-started-with-nodejs#dyno-sleeping-and-sca ...

  2. Android dex分包方案和热补丁原理

    一.分包的原因: 当一个app的功能越来越复杂,代码量越来越多,也许有一天便会突然遇到下列现象: 1. 生成的apk在2.3以前的机器无法安装,提示INSTALL_FAILED_DEXOPT 2. 方 ...

  3. Javascript原型与对象等知识

    声明式函数定义: function add(m,n) { alert(m+n); } 这种方式等同于构造一个Function类的实例的方式: var add = new Function(" ...

  4. cxgrid取消过滤下拉框

    选择tableview1.optionscustomize.columnfiltering=fasle;

  5. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  6. Postgres数据库在Linux中优化

    I/O 优化1 打开 noatime nodirtime,async 方法: 修改 /etc/fstab stat 命令查看 2 调整预读方法: 查看 sudo blockdev --getra /d ...

  7. SoapUI设置Cookie

    因為.NET寫的Web Service的方法是需要驗證session的. 需要先call方法Login之後才能使用其它的方法.最近剛在學用SoapUI測試soap的API,剛好可以通過Groovy S ...

  8. Mysql链接字符串问题

    <add key="ConnstringMySql" value="server=xxx.xxx.xxx.xxx;database=YourDatabase;uid ...

  9. vim 插入时间戳的方法

    这里主要说明用内置函数 strftime 来插入,而不用 :r!date 或类似方法. 用命令 "=strftime('%c')<Ret>p ,或<C-r>=strf ...

  10. android相对布局中控件的常用属性

    Android布局属性详解 RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHorizontal 水平居中 andr ...