当前位置:首页 > 使用IConfigurationSectionHandler在web.config中增加自定义配置

使用IConfigurationSectionHandler在web.config中增加自定义配置

点击次数:1241  更新日期:2010-12-30
\n

VS2008、ASP.NET 3.5


\n

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


\n

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


\n

2. 实现IConfigurationSectionHandler接口:SiteSettingHandler


\n

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


\n

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 };
}


\n

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


\n

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


\n

<siteSetting>
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>


\n

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


\n

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


\n

namespace Tristan.SeeCustomConfig {


\n

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


\n

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


\n

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


\n

2. 修改SiteSettingHandler


\n

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


\n

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 };


\n

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


\n

#endregion
}
}


\n

3. 修改web.config中的配置


\n

<siteSetting type=”Tristan.SeeCustomConfig.SiteSetting”>
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>


\n

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


\n

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

\n