使用IConfigurationSectionHandler在web.config中增加自定义配置
时间:2011-04-07 来源:刺猬的温驯
一. 场景
这里仅举一个简单应用的例子,我希望在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; }
}
}
namespace Tristan.SeeCustomConfig {
public class SiteSettingHandler : IConfigurationSectionHandler {
#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
}
}
public class SiteSetting {
public string SiteName { get; set; }
public string SiteVersion { get; set; }
public bool Closed { get; set; }
}
}
2. 实现IConfigurationSectionHandler接口:SiteSettingHandler
namespace Tristan.SeeCustomConfig {
public class SiteSettingHandler : IConfigurationSectionHandler {
#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中进行配置
在<configSections></configSections>里面增加一个节点:
<section name="siteSetting" type="Tristan.SeeCustomConfig.SiteSettingHandler"/>
name:指定我们将要增加的节点名为"siteSetting",接下来会使用它来编写配置节点
type:指定处理这个配置节点的handler,这个类,我们在前面已经把代码实现了
然后在<configuration><configuration>里面增加一段xml:
<siteSetting>
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>
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());
}
}
}
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; }
}
}
namespace Tristan.SeeCustomConfig {
public class SiteSettingHandler : IConfigurationSectionHandler {
IConfigurationSectionHandler Members 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
}
}
[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
namespace Tristan.SeeCustomConfig {
public class SiteSettingHandler : IConfigurationSectionHandler {
IConfigurationSectionHandler Members 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中的配置
<siteSetting type="Tristan.SeeCustomConfig.SiteSetting">
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>
4. 再来看看
不修改测试代码,得到了一样的效果 :)
相关阅读 更多 +