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

Reading number is top 10 articles
数据库的备份与还原_[SQL,Server教程]
asp.net2.0内置Response对象的属性
SQL Server2005SQLCLR代码安全之权限_[SQL Server教程]
ASP.NET程序与Access和SQL,Server数据库连接_[Asp.Net教程]
用javascript实现评估用户输入密码的强度_JavaScript技术_编程技术
PHP入门:初学来看配置文件PHP.INI的中文注释_php资料_编程技术
C#中Label控件应用实例
整站如何防止SQL注入攻击_[Asp.Net教程]
简单的SQL语句摘记_[SQL,Server教程]
怎样在PHP,中运用,SimpleXML,处理任务_php资料_编程技术
Reading number is top 10 pictures
More attractive than sora aoi3
Absolutely shocked. National geographic 50 animal photographys6
去瑜伽会所面试的经过
男人们都在想什么
Take you to walk into the most true north Korea rural1
白嫩丰满美女照片
Fierce! China's special forces training the devil2
清纯性感的美眉1
青春清纯美女大集合1
恶搞漫画2
Download software ranking
Unix video tutorial2
传奇私服架设教程-chm
Boxer vs Yellow4
打鸟视频
金山office2007
Sora aoi - one of more PK
Eclipse-CALMSANNY (second edition)
Prostitutes diary
网页特效实例大全
C#程序员参考手册
归海一刀 published in(发表于) 2014/1/30 1:07:50 Edit(编辑)
使用IConfigurationSectionHandler在web.config中增加自定义配置_[Asp.Net教程]

使用IConfigurationSectionHandler在web.config中增加自定义配置_[Asp.Net教程]

使用IConfigurationSectionHandler在web.config中增加自定义配置_[Asp.Net教程]

VS2008、ASP.NET 3.5


一. 场景
这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等。
二. 基本实现方法
1. 定义配置节点对应的类:SiteSetting
代码片段:


namespace Tristan.SeeCustomConfig {
public class SiteSetting {
public string SiteName { get; set; }
public string SiteVersion { get; set; }
public bool Closed { get; set; }
}
}


2. 实现IConfigurationSectionHandler接口:SiteSettingHandler


Code
namespace Tristan.SeeCustomConfig {
public class SiteSettingHandler : IConfigurationSectionHandler {
IConfigurationSectionHandler Members#region IConfigurationSectionHandler Members


public object Create(object parent, object configContext, System.Xml.XmlNode section) {
string siteName = section.SelectSingleNode("siteName").InnerText;
string siteVersiton = section.SelectSingleNode("siteVersion").InnerText;
bool closed = Convert.ToBoolean(section.SelectSingleNode("closed").InnerText);
return new SiteSetting() { SiteName = siteName, SiteVersion = siteVersiton };
}


#endregion
}
}
3. 在web.config中进行配置
里面增加一个节点:



name:指定我们将要增加的节点名为"siteSetting",接下来会使用它来编写配置节点
type:指定处理这个配置节点的handler,这个类,我们在前面已经把代码实现了
然后在里面增加一段xml:



遇见未来
1.0
false


4. 看看效果吧
随便建一个页面在后台代码里写几行代码做个测试:


namespace Tristan.SeeCustomConfig {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
SiteSetting site = ConfigurationManager.GetSection("siteSetting") as SiteSetting;
Response.Write(site.SiteName + "," + site.SiteVersion + "," + site.Closed.ToString());
}
}
}
运行,可以看到,我们在web.config中的信息被write出来了。 :)
三. 使用XML反序列化
1. 修改SiteSetting


namespace Tristan.SeeCustomConfig {


[Serializable]
[XmlRoot("siteSetting")]
public class SiteSetting {
[XmlElement("siteName",typeof(string))]
public string SiteName { get; set; }


[XmlElement("siteVersion",typeof(string))]
public string SiteVersion { get; set; }


[XmlElement("closed",typeof(Boolean))]
public bool Closed { get; set; }
}
}


2. 修改SiteSettingHandler


Code
namespace Tristan.SeeCustomConfig {
public class SiteSettingHandler : IConfigurationSectionHandler {
IConfigurationSectionHandler Members#region IConfigurationSectionHandler Members


public object Create(object parent, object configContext, System.Xml.XmlNode section) {
//string siteName = section.SelectSingleNode("siteName").InnerText;
//string siteVersiton = section.SelectSingleNode("siteVersion").InnerText;
//bool closed = Convert.ToBoolean(section.SelectSingleNode("closed").InnerText);
//return new SiteSetting() { SiteName = siteName, SiteVersion = siteVersiton };


string typeName = ((XmlElement)section).GetAttribute("type");
XmlSerializer xz = new XmlSerializer(Type.GetType(typeName));
using (StringReader sr = new StringReader(section.OuterXml)) {
return xz.Deserialize(sr);
}
}


#endregion
}
}


3. 修改web.config中的配置



遇见未来
1.0
false


4. 再来看看
不修改测试代码,得到了一样的效果 :)


来源: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.