使用IConfigurationSectionHandler在web.config中增加自定义配置
一. 场景
这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等。
二. 基本实现方法
需要建立一个类项目Tristan.SeeCustomConfig,和一个Web站点用来测试,如下所示
1. 定义配置节点对应的类:SiteSetting
代码片段:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RobsunConfigSectionHandler
{
public class RobsunPara
{
#region code
private string _path = "";
private string _companyName = "";
private bool _isPrivate = false;
public string Path
{
get{ return _path; }
set{ _path = value; }
}
public string CompanyName
{
get{ return _companyName; }
set{ _companyName = value; }
}
public bool IsPrivate
{
get{ return _isPrivate; }
set{ _isPrivate = value; }
}
#endregion
}
}
2. 实现IConfigurationSectionHandler接口:SiteSettingHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
namespace RobsunConfigSectionHandler
{
public class RobsunParaSectionHandler:IConfigurationSectionHandler
{
public static string path = string.Empty;
public object Create(object parent, object configContext, XmlNode section)
{
RobsunPara para = new RobsunPara();
foreach (XmlNode xn in section.ChildNodes)
{
switch (xn.Name)
{
case "path":
para.Path = xn.SelectSingleNode("@value").InnerText;
path = para.Path;
break;
case "companyName":
para.CompanyName = xn.SelectSingleNode("@value").InnerText;
break;
case "isPrivate":
para.IsPrivate = bool.Parse(xn.SelectSingleNode("@attribute").InnerText);
break;
}
}
return para;
}
}
}
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>
完整代码见下图:
4. 看看效果吧
随便建一个页面在后台代码里写几行代码做个测试:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; //导入命名空间 using Tristan.SeeCustomConfig; using System.Configuration; namespace WebApplication1 { 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()); } } }
运行结果:
遇见未来,1.0,False
三. 使用XML反序列化
1. 修改SiteSetting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//导入命名空间
using System.Xml.Serialization;
namespace Tristan.SeeCustomConfig
{
//public class SiteSetting
//{
// public string SiteName { get; set; }
// public string SiteVersion { get; set; }
// public bool Closed { get; set; }
//}
[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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//导入命名空间
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace Tristan.SeeCustomConfig
{
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 };
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"
4. 再来看看
不修改测试代码,得到了一样的效果 :)
参考:
http://blog.****.net/zyc21st/archive/2004/12/09/210990.aspx