ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-中)
我们就接着上一篇继续说,上一篇中介绍了ConfigSection的结构和两个简单的DEMO,本篇就说一下SectionGroup、ConfigurationElementCollection和key/value pair configurationsection.
的使用。
1、SectionGroup的使用
下面的代码简单的说明一下SectionGroup的使用:
1)、首先定义一个继承ConfigurationSectionGroup的类:
1: /// <summary>
2: /// 自定义SectionGroup
3: /// </summary>
4: public class MySectionGroup:ConfigurationSectionGroup
5: {
6: [ConfigurationProperty("myBlog")]
7: public MySection MyBlog
8: {
9: get
10: {
11: return (MySection)base.Sections["myBlog"];
12: }
13: }
14: }
2)、其次,在定义一个继承ConfigurationSection的Section:
1: /// <summary>
2: /// 自定义Section
3: /// </summary>
4: public class MySection:ConfigurationSection
5: {
6: [ConfigurationProperty("name")]
7: public string BlogName
8: {
9: get
10: {
11: return (string)base["name"];
12: }
13: }
14: [ConfigurationProperty("url")]
15: public string BlogUrl
16: {
17: get
18: {
19: return (string)base["url"];
20: }
21: }
22: }
下面再讨论一下怎么在web.config中如何配置和使用:
web.config中的配置:
1: <configSections>
2: <sectionGroup name="myBlogs" type="KevinDiao.MySectionDemo01.MySectionGroup,KevinDiao.MySectionDemo01">
3: <section name="myBlog" type="KevinDiao.MySectionDemo01.MySection,KevinDiao.MySectionDemo01"/>
4: </sectionGroup>
5: </configSections>
6:
7: <myBlogs>
8: <myBlog name="五香瓜子" url="http://www.cnblogs.com/diaojia/"></myBlog>
9: </myBlogs>
读取web.config中配置:
1: MySection mySection = ConfigurationManager.GetSection("myBlogs/myBlog") as MySection;
2: Response.Write("博客名称:" + mySection.BlogName + "<br/>");
3: Response.Write("博客地址:<a href='" + mySection.BlogUrl + "'>" + mySection.BlogUrl + "</a>");
运行得到的结果:
博客名称:五香瓜子
博客地址:http://www.cnblogs.com/diaojia/
2、ConfigurationElementCollection的使用
下面再来讨论一下怎么在ConfigSections中配置自定义集合,我们还是用代码说明吧。
1)、 首先定义一个继承ConfigurationElement的类。
1: /// <summary>
2: /// 自定义Element
3: /// </summary>
4: public class MyBlogElement:ConfigurationElement
5: {
6: [ConfigurationProperty("name")]
7: public string Name
8: {
9: get
10: {
11: return (string)base["name"];
12: }
13: }
14: [ConfigurationProperty("url")]
15: public string Url
16: {
17: get
18: {
19: return (string)base["url"];
20: }
21: }
22: }
它主要包含要配置的主要内容。
2)、其次定义一个继承ConfigurationElementCollection的类
1: /// <summary>
2: /// 自定义ElementCollection
3: /// </summary>
4: public class MyBlogElementCollection:ConfigurationElementCollection
5: {
6: protected override ConfigurationElement CreateNewElement()
7: {
8: return new MyBlogElement();
9: }
10: protected override object GetElementKey(ConfigurationElement element)
11: {
12: return ((MyBlogElement)element).Name;
13: }
14: public override ConfigurationElementCollectionType CollectionType
15: {
16: get
17: {
18: return ConfigurationElementCollectionType.BasicMap;
19: }
20: }
21:
22: protected override string ElementName
23: {
24: get
25: {
26: return "myBlog";
27: }
28: }
29: public MyBlogElement this[int index]
30: {
31: get
32: {
33: return (MyBlogElement)BaseGet(index);
34: }
35: set
36: {
37: if (BaseGet(index) != null)
38: {
39: BaseRemoveAt(index);
40: }
41: BaseAdd(index, this);
42: }
43: }
44: }
3)、再次定义一个继承ConfigurationSection的类
1: /// <summary>
2: /// 自定义Section
3: /// </summary>
4: public class MyBlogSection:ConfigurationSection
5: {
6: [ConfigurationProperty("name")]
7: public string Name
8: {
9: get
10: {
11: return (string)base["name"];
12: }
13: }
14: [ConfigurationProperty("",IsDefaultCollection=true)]
15: public MyBlogElementCollection MyBlogCollection
16: {
17: get
18: {
19: return (MyBlogElementCollection)base[""];
20: }
21: }
22:
23: }
下面在看看怎么在web.config中如何配置:
1: <configSections>
2: <section name ="MyBlogs" type="KevinDiao.MySectionDemo02.MyBlogSection,KevinDiao.MySectionDemo02"/>
3: </configSections>
4: <MyBlogs name="KevinDiao">
5: <myBlog name="五香瓜子" url="http://www.cnblogs.com/diaojia/"></myBlog>
6: <myBlog name="业精于勤" url="http://diaojia.blog.51cto.com/"></myBlog>
7: </MyBlogs>
再看看怎么读取:
1: MyBlogSection mySection = ConfigurationManager.GetSection("MyBlogs") as MyBlogSection;
2: foreach (MyBlogElement item in mySection.MyBlogCollection)
3: {
4: Response.Write("博客名称:" + item.Name + "<br/>");
5: Response.Write("博客地址:<a href='" + item.Url + "'>" + item.Url + "</a>" + "<br/>");
6: Response.Write("-----------------------------------------------------------------<br/>");
7: }
最后在看看运行的结果:
博客名称:五香瓜子
博客地址:http://www.cnblogs.com/diaojia/
-----------------------------------------------------------------
博客名称:业精于勤
博客地址:http://diaojia.blog.51cto.com/
-----------------------------------------------------------------
3、key/value pair configurationsection的使用(例如:appSettings)
最后在简单的说明一下在web.config中怎么使用key/value pair configurationsection。
1)、首先定义一个继承ConfigurationElement的类
1: /// <summary>
2: /// 自定义ConfigurationElement
3: /// </summary>
4: public class MyBlogElement: ConfigurationElement
5: {
6: [ConfigurationProperty("name")]
7: public string Name
8: {
9: get
10: {
11: return (string)base["name"];
12: }
13: }
14: [ConfigurationProperty("url")]
15: public string Url
16: {
17: get
18: {
19: return (string)base["url"];
20: }
21: }
22: }
其实和在2、ConfigurationElementCollection的使用中的差不多。
2)、其次在定义一个继承ConfigurationElementCollection的类:
1: /// <summary>
2: /// 自定义ConfigurationElementCollection
3: /// </summary>
4: public class MyBlogsElectionCollection:ConfigurationElementCollection
5: {
6: protected override ConfigurationElement CreateNewElement()
7: {
8: return new MyBlogElement();
9: }
10:
11: protected override object GetElementKey(ConfigurationElement element)
12: {
13: return ((MyBlogElement)element).Name;
14: }
15: }
3)、再次就是定义一个继承ConfigurationSection的类
1: /// <summary>
2: /// 自定义Section
3: /// </summary>
4: public class MyBlogsSection : ConfigurationSection
5: {
6: [ConfigurationProperty("name")]
7: public string Name
8: {
9: get
10: {
11: return (string)base["name"];
12: }
13: }
14: [ConfigurationProperty("",IsDefaultCollection=true)]
15: public MyBlogsElectionCollection MyBlogs
16: {
17: get
18: {
19: return (MyBlogsElectionCollection)base[""];
20: }
21: }
22: }
下面在看看怎么在web.config中如何配置:
1: <configSections>
2: <section name="MyBlogs" type="KevinDiao.MySectionDemo03.MyBlogsSection,KevinDiao.MySectionDemo03"/>
3: </configSections>
4: <MyBlogs name="KevinDiao">
5: <add name="五香瓜子" url="http://www.cnblogs.com/diaojia/"></add>
6: <add name="业精于勤" url="http://diaojia.blog.51cto.com/"></add>
7: </MyBlogs>
再看看怎么读取:
1: MyBlogsSection mySection = ConfigurationManager.GetSection("MyBlogs") as MyBlogsSection;
2: foreach (MyBlogElement item in mySection.MyBlogs)
3: {
4: Response.Write("博客名称:" + item.Name + "<br/>");
5: Response.Write("博客地址:<a href='" + item.Url + "'>" + item.Url + "</a>" + "<br/>");
6: Response.Write("-----------------------------------------------------------------<br/>");
7: }
最后在看看运行的结果:
博客名称:五香瓜子
博客地址:http://www.cnblogs.com/diaojia/
-----------------------------------------------------------------
博客名称:业精于勤
博客地址:http://diaojia.blog.51cto.com/
-----------------------------------------------------------------
最后在附上本篇的代码:DEMO
REFERENCE FROM : http://www.cnblogs.com/diaojia/archive/2011/04/06/2007350.html
参考:
MSDN
ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-中)相关推荐
- ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 )
ConfigSections的结构 首先我们先回顾一下ConfigSections的结构和它子节点的说明,如下: 1: <configSections> 2: <sectionGro ...
- ASP.NET 4.0 新特性--Web.Config Transformation(原创)
. 系列文章 Web.Config Transformation详解,这部分内容比较简单,关键是用没有用过的问题,所以这里希望帮助大家实践一下. 一 概述: 在VS2010中引入在Config 文件中 ...
- (转)asp.net夜话之十一:web.config详解
在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代 ...
- asp.net夜话之十一:web.config详解
在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代 ...
- 【ASP.NET】Global.asax与Web.config
背景 在创建网站的项目,总是会看到Web.config这个文件,它是用来干什么的呢?还有咱们自己也可以新建Global.asax文件,它是用来干什么的呢.在我实现一个网页上显示历史访问人数和在线人数的 ...
- SAE上传web应用(包括使用数据库)教程详解及问题解惑
2019独角兽企业重金招聘Python工程师标准>>> 转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 ...
- .net System.Web.Caching.Cache缓存类使用详解(转载)
转自:http://www.cnblogs.com/virusswb/articles/1681561.html net System.Web.Caching.Cache缓存类使用详解 System. ...
- Web IDE优势在哪?详解Web版数据库管理工具SQL Studio
Web IDE优势在哪?详解Web版数据库管理工具SQL Studio 去年年末,GitPod在A轮融资中获得2500万美元,用来兑现云端开发环境(Cloud Development Environm ...
- Web安全—安全事件的分析思路详解(安全设备)
Web安全-安全事件的分析思路详解(安全设备) 提要:不管是在平常的安全设备运维,还是HVV,还是蓝队等,安全设备(WAF,防火墙,IDS,IPS,态势感知等)的日志分析都尤为重要. 安全事件的分析思 ...
最新文章
- 开发机器上利用vs2013调试远程IIS上的c#程序
- 112页数学知识整理!机器学习-数学基础回顾.pptx
- 04_Java面向对象特征之继承与多态
- C# 重写WndProc 消息循环
- 顺利达成微软HacktoberFest 2018
- c js php比较字符串,Php 比较字符串相像度
- ax3000 梅林_梅林甘蔗下种忙 古法红糖“熬”成致富新产业
- ssm架构 开源项目_如何为您的开源项目选择正确的品牌架构
- 【干货】无人机如何进行倾斜摄影的航线规划
- linux sql server调优,SQL SERVER性能优化(转)
- 修改Maven默认编译级别
- 2018.09.02 bzoj1025: [SCOI2009]游戏(计数dp+线筛预处理)
- matlab/simulink实现QPSK调制和解调实验
- html5在线画板菱形怎么画,HTML5 Canvas 制作一个“在线画板”
- c语言:数组插入处理
- Google Adsense(Google网站联盟)广告申请指南
- matlab函数sim,MATLAB 中sim函数
- shell grep正则匹配汉字
- TCP-面向连接的、可靠的、基于字节流的 传输层通信协议
- 火影_青鸟_中日罗马音
热门文章
- python编程计算1!+2!+...+10!_如何用C语言编程计算 1!+2!+3!+…+10!?
- 《It's All Upside Down》作者访谈录
- MySQL解压缩安装
- linux学习笔记 第七篇 (samba(一))(iscsi)
- mybatis多表分页
- 解决Outlook带有附件的邮件重复发送的问题
- innodb参数汇总
- (转)nginx+iis实现负载均衡
- Java Servlet关键点详解
- javascript学习(三) 内置对象