我们就接着上一篇继续说,上一篇中介绍了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详解-中)的更多相关文章

  1. ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-上 )

    ConfigSections的结构 首先我们先回顾一下ConfigSections的结构和它子节点的说明,如下: 1: <configSections> 2: <sectionGro ...

  2. ASP.NET杂谈-一切都从web.config说起(2)(ConfigSections详解-下)

    还是接着上一篇说起,在上两篇中主要和大家探讨了ConfigSection的几种常用形式,并举例几个例子说明了一下.其实它们主要都是继承System.Configuration.Configuratio ...

  3. asp.net夜话之十一:web.config详解

    转:http://blog.csdn.net/zhoufoxcn/article/details/3265141 在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我 ...

  4. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)

    10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides Wednesday, Janua ...

  5. WCF项目问题2-无法激活服务,因为它需要 ASP.NET 兼容性。没有未此应用程序启用 ASP.NET 兼容性。请在 web.config 中启用 ASP.NET 兼容性,或将 AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode 属性设置为 Required 以外的值。

    无法激活服务,因为它需要 ASP.NET 兼容性.没有未此应用程序启用 ASP.NET 兼容性.请在 web.config 中启用 ASP.NET 兼容性,或将 AspNetCompatibility ...

  6. HTTP 错误 500.19 – Internal Server Error web.config 文件的 system.webServer/httpErrors 节中不允许绝对物理路径“C:\inetpub\custerr”[转]

    给ASP或者ASP.NET等需要配置IIS服务器的过程中,很可能会遇到以下两种错误.尤其是用Win7系统的,配置IIS7.0版本比用XP系统配置IIS5.1版本而言要复杂复杂一些.当同时需要配置ASP ...

  7. SAE上传web应用(包括使用数据库)教程详解及问题解惑

    转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 SAE上传web应用(包括使用数据库)教程详解及问题解惑: 最近由于工作 ...

  8. ASP.NET MVC 5 学习教程:Edit方法和Edit视图详解

    原文 ASP.NET MVC 5 学习教程:Edit方法和Edit视图详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 ...

  9. 【译】ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解

    原文:[译]ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解 在本节中,我们继续研究生成的Edit方法和视图.但在研究之前,我们先将 release date 弄得好看一点.打 ...

随机推荐

  1. 【温故而知新-Javascript】使用 Ajax

    Ajax 是现代Web 应用程序开发的一项关键工具.它让你能向服务器异步发送和接收数据,然后用 Javascript 解析. Ajax 是 Asynchronous JavaScript and XM ...

  2. (三)策略模式-C++实现

    策略模式:定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换,本模式使得算法可独立于使用它的客户而变化. 三种角色: 1.策略:一个抽象类,这个接口定义了若干个算法标识,即多个虚函数,这些个 ...

  3. ural One-two, One-two 2

     One-two, One-two 2 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  4. AC日记——欧几里得的游戏 洛谷 P1290

    题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数 ...

  5. AC日记——删除单词后缀 openjudge 1.7 20

    20:删除单词后缀 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个单词,如果该单词以er.ly或者ing后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为0), 否 ...

  6. Hibernate一对一关系映射

    Hibernate提供了两种一对一映射关联关系的方式: 1)按照外键映射 2)按照主键映射 下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映 ...

  7. 在Ubuntu14.04下安装vsftp服务器

    猜想在Ubuntu下搭建ftp服务器来实现windows和ubuntu下文件互传是一件很简单的事儿,但是在网上找了好几篇文章都不行,故自己在这里总结一下方法. 首先安装vsftp服务器 sudo ap ...

  8. vijos 1512

    SuperBrother打鼹鼠 背景 SuperBrother在机房里闲着没事干(再对比一下他的NOIP,真是讽刺啊......),于是便无聊地开始玩“打鼹鼠”...... 描述 在这个“打鼹鼠”的游 ...

  9. js模拟高级语言的重载

    js以递归的方式模拟高级语言的重载,我以添加元素节点为例子: //现有的子元素之前插入一个新的子元素 var before = function(elem,newElement,targetEleme ...

  10. HealthKit框架

    HealthKit框架相关资料 链接: HealthKit框架参考 HealthKit开发快速入门教程之HealthKit数据的操作 HealthKit开发快速入门教程之HealthKit框架体系创建 ...