All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET底层架构之从浏览器到ASP.NET_.net资料_编程技术
在ASP.NET中实现Model,View,Controller模式(1)_[Asp.Net教程]
异常:操作必须使用一个可更新的查询。_[Asp.Net教程]
C#教程:.NET Framework简介
借助HiddenText,确定CheckBoxList当前的操作类型及点击的CheckBox_[Asp.Net教程]
C#教程:DLL动态链接库的优点
C#应用:读取指定盘符的硬盘序列号_[Asp.Net教程]
通过ADO.NET实现事务处理_[Asp.Net教程]
一个Ajax.NET的查询实例_[Asp.Net教程]
PHP安全配置之实现安全的两个重点_php资料_编程技术
Reading number is top 10 pictures
初五接财神啦!五路财神齐来到
影评-疯子,我爱你
The world's ten biggest attractions of inventory super the moon
徐若瑄展示美丽胸围1
Nine school beauty star those gossip matters
A man's favorite things9
The money of more than 100 countries and regions8
运动的范冰冰3
Sora aoi possession photo2
Chinese paper-cut grilles art appreciation4
Download software ranking
Unix video tutorial5
尖东毒玫瑰B
徐若瑄成名作“魔鬼天使”
Ashlynn Video1
The cock of the Grosvenor LTD handsome
WebService在.NET中的实战应用教学视频 → 第3集
apache-tomcat-6.0.33
JSP+Ajax Web development typical examples
Tram sex maniac 2 (H) rar bag1
Boxer's Top ten classic battle10
归海一刀 published in(发表于) 2014/1/30 1:13:59 Edit(编辑)
通过继承ConfigurationSection,在web.config中增加自定义配置_[Asp.Net教程]

通过继承ConfigurationSection,在web.config中增加自定义配置_[Asp.Net教程]

通过继承ConfigurationSection,在web.config中增加自定义配置_[Asp.Net教程]

VS2008、Asp.Net 3.5


前几天写了一篇使用IConfigurationSectionHandler在web.config中增加自定义配置,然后看到有回复说IConfigurationSectionHandler在2.0时代被弃用了,推荐使用ConfigurationSection,不敢怠慢,赶紧看看ConfigurationSecion是如何使用的。


1. 实现目的
我希望在web.config中,配置网站信息,管理员信息,和用户信息(当然这个配置有点不切实际了,这里只是为了演示),所以,我希望按下面的格式做配置。






这个siteSetting配置节点是一个稍微复杂一点的配置,自己包含有Attributes,同时包含子节点siteAdmin, siteUsers, 而siteUsers又包含多个siteUser子节点。
接下来我要定义几个类,分别代表各个不同的节点。有一定的规则:
代表根节点,也就是siteSetting节点的类,继承自ConfigurationSection类
代表单一子节点的siteAdmin, siteUser类,继承自ConfigurationElement类
包含多个同名子节点的siteUsers类,继承自ConfigurationElementCollection类
2. SiteAdmin类
public class SiteAdmin : ConfigurationElement
{
[ConfigurationProperty("adminId", IsRequired=true)]
public int AdminId {
get { return Convert.ToInt32(this["adminId"]); }
set { this["adminId"] = value; }
}
[ConfigurationProperty("adminName")]
public string AdminName {
get { return this["adminName"] as string; }
set { this["adminName"] = value; }
}
} 注意ConfigurationPropertyAttribute,和this关键字,很明显在基类中定义了索引器。本文并不想对这些做过多探讨,直接以代码展示。
3. SiteUser类
public class SiteUser : ConfigurationElement {


[ConfigurationProperty("userId",IsRequired=true)]
public int UserId {
get { return Convert.ToInt32(this["userId"]); }
set { this["userId"] = value; }
}
[ConfigurationProperty("userName")]
public string UserName {
get { return this["userName"] as string; }
set { this["userName"] = value; }
}
}
4. SiteUsers类
public class SiteUsers : ConfigurationElementCollection {


protected override ConfigurationElement CreateNewElement() {
return new SiteUser();
}


protected override object GetElementKey(ConfigurationElement element) {
SiteUser siteUser = element as SiteUser;
return siteUser.UserId;
}


public override ConfigurationElementCollectionType CollectionType {
get {
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName {
get {
return "siteUser";
}
}
} 继承自ConfigurationElementCollection的类,必须override以上4个方法。
SiteUsers是SiteUser的集合,因此不难理解上述4个override方法的目的。
5. SiteSetting类
public class SiteSetting : ConfigurationSection {

[ConfigurationProperty("siteName")]
public string SiteName {
get { return this["siteName"] as string; }
set { this["siteName"] = value; }
}
[ConfigurationProperty("siteVersion")]
public string SiteVersion {
get { return this["siteVersion"] as string; }
set { this["siteVersion"] = value; }
}
[ConfigurationProperty("closed", IsRequired=true)]
public bool Closed {
get { return (bool)this["closed"]; }
set { this["closed"] = value; }
}


[ConfigurationProperty("siteAdmin")]
public SiteAdmin SiteAdmin {
get { return this["siteAdmin"] as SiteAdmin; }
set { this["siteAdmin"] = value; }
}
[ConfigurationProperty("siteUsers")]
public SiteUsers SiteUsers {
get { return this["siteUsers"] as SiteUsers; }
}
}
6. 在web.config添加我们的自定义配置
根据我们最初的设想,现在来对web.config进行配置
中加入:


中加入:







7. 检验结果
这样就完成了吗?是的。
来写简单的测试代码,将我们的自定义配置信息输出来:
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {


SiteSetting siteSetting = ConfigurationManager.GetSection("siteSetting") as SiteSetting;


Response.Write(siteSetting.SiteName + "," + siteSetting.SiteVersion + "," + siteSetting.Closed.ToString());
Response.Write("
");


Response.Write(siteSetting.SiteAdmin.AdminId.ToString() + "," + siteSetting.SiteAdmin.AdminName);
Response.Write("
");


foreach (SiteUser u in siteSetting.SiteUsers) {
Response.Write(u.UserId.ToString() + "," + u.UserName);
Response.Write("
");
}
}
}
测试通过:)

再联想之前使用IConfigurationSectionHandler,我觉得比本文描写的方法更好用,为什么?因为更容易理解啊,只需实现一个接口,不像这个,要根据不同的情况分别继承那么几个类。
如果IConfigurationSectionHandler果真在2.0里不推荐使用,那么却又在3.5中恢复身份,也是可以理解的。


来源:http://www.cnblogs.com/guozhijian/







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.