日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2025/7/25 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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详解-中)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。