实现目的
我希望在web.config 中,配置网站信息,管理员信息,和用户信息(当然这个配置有点不切实际了,这里只是为了演示),所以,我希望按下面的格式做配置。
<siteSetting siteName="遇见未来" siteVersion="1.0" closed="false">
<siteAdmin adminId="1" adminName="guozhijian"/>
<siteUsers>
<siteUser userId="1" userName="zhanglanzhen"/>
<siteUser userId="2" userName="wdy"/>
</siteUsers>
</siteSetting>
这个siteSetting配置节点是一个稍微复杂一点的配置,自己包含有Attributes,同时包含子节点siteAdmin, siteUsers, 而siteUsers又包含多个siteUser子节点。
接下来我要定义几个类,分别代表各个不同的节点。有一定的规则:
代表根节点,也就是siteSetting节点的类,继承自ConfigurationSection 类
代表单一子节点的siteAdmin, siteUser类,继承自ConfigurationElement 类
包含多个同名子节点的siteUsers类,继承自ConfigurationElement Collection类
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"; }
}
}
继承自ConfigurationElement Collection的类,必须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 进行配置
在<configSections></configSections>中加入:
<section name="siteSetting" type="Tristan.SeeCustomCfg.SiteSetting"/>
在<configuration></configuration>中加入:
<siteSetting siteName="遇见未来" siteVersion="1.0" closed="false">
<siteAdmin adminId="1" adminName="guozhijian"/>
<siteUsers>
<siteUser userId="1" userName="zhanglanzhen"/>
<siteUser userId="2" userName="wdy"/>
</siteUsers>
</siteSetting>
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("<br/>");
Response.Write(siteSetting.SiteAdmin.AdminId.ToString() + "," + siteSetting.SiteAdmin.AdminName);
Response.Write("<br/>");
foreach (SiteUser u in siteSetting.SiteUsers)
{
Response.Write(u.UserId.ToString() + "," + u.UserName);
Response.Write("<br/>");
}
}
}