.Net 2.0 中的自定义配置节的处理程序有了点变化,不用再实现System.Configuration.IConfigurationSectionHandler接口,而是要继承 System.Configuration.ConfigurationSection 类。先来看个简单点的实现:
在web.config中,有如下的配置:
<configSections>
<section name="CustomerProviders" type="MyConfigSectionHandler.MyHandler" />
</configSections>
<CustomerProviders>
<Provider Name="QingProvider" File="FileName" />
</CustomerProviders>
<section name="CustomerProviders" type="MyConfigSectionHandler.MyHandler" />
</configSections>
<CustomerProviders>
<Provider Name="QingProvider" File="FileName" />
</CustomerProviders>
很简单,只有一个属性,此处关键是把相应的属性对应好.
public class MyHandler : ConfigurationSection
{
public MyHandler() { }
[ConfigurationProperty("Provider")] //对应于Providers结点
public MyProvider Provider
{
get { return (MyProvider)this["Provider"]; }
set { this["Provider"] = value; }
}
}
public class MyProvider : ConfigurationElement
{
[ConfigurationProperty("Name")] //对应于Name属性
public string Name
{
get { return (string)this["Name"]; }
set { this["Name"] = value; }
}
[ConfigurationProperty("File")] //对应于File属性
public string File
{
get { return (string)this["File"]; }
set { this["File"] = value; }
}
}
{
public MyHandler() { }
[ConfigurationProperty("Provider")] //对应于Providers结点
public MyProvider Provider
{
get { return (MyProvider)this["Provider"]; }
set { this["Provider"] = value; }
}
}
public class MyProvider : ConfigurationElement
{
[ConfigurationProperty("Name")] //对应于Name属性
public string Name
{
get { return (string)this["Name"]; }
set { this["Name"] = value; }
}
[ConfigurationProperty("File")] //对应于File属性
public string File
{
get { return (string)this["File"]; }
set { this["File"] = value; }
}
}
这一切做好之后,使用就非常简单了
MyConfigSectionHandler.MyHandler config =
(MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection("CustomerProviders");
Response.Write(config.Provider.Name + "<br>" + config.Provider.File);
(MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection("CustomerProviders");
Response.Write(config.Provider.Name + "<br>" + config.Provider.File);
下面来看一个复杂点的,比如我们要写一个URL Rewrite的配置。在web.config中做了这样的配置:
<configSections>
<section name="RewriteRules" type="MyConfigSectionHandler.MyHandler" />
</configSections>
<RewriteRules>
<Rule>
<add LookFor="~/Software.aspx" SendTo="Catagory.aspx?CategoryID=1 "/>
<add LookFor="~/Hardware.aspx" SendTo="Catagory.aspx?CategoryID=2 "/>
<add LookFor="~/Services.aspx" SendTo="Catagory.aspx?CategoryID=3 "/>
</Rule>
</RewriteRules>
<section name="RewriteRules" type="MyConfigSectionHandler.MyHandler" />
</configSections>
<RewriteRules>
<Rule>
<add LookFor="~/Software.aspx" SendTo="Catagory.aspx?CategoryID=1 "/>
<add LookFor="~/Hardware.aspx" SendTo="Catagory.aspx?CategoryID=2 "/>
<add LookFor="~/Services.aspx" SendTo="Catagory.aspx?CategoryID=3 "/>
</Rule>
</RewriteRules>
public class MyHandler : ConfigurationSection
{
public MyHandler() { }
[ConfigurationProperty("Rule")]
public RulesCollection Rule //把Rule映射为一个配置元素的集合类
{
get { return (RulesCollection)this["Rule"]; }
set { this["Rule"] = value; }
}
}
//此处只是重写了这个类的几个最基本的的方法
public class RulesCollection : ConfigurationElementCollection
{
public Rule this[int index]
{
get { return (Rule)BaseGet(index); }
set
{
if (BaseGet(index) != null) { BaseRemoveAt(index); }
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Rule();
}
protected override object GetElementKey(ConfigurationElement element)
{
return element.GetHashCode();
}
}
public class Rule : ConfigurationElement
{
public Rule() { }
[ConfigurationProperty("LookFor")]
public String LookFor
{
get { return (string)this["LookFor"]; }
set { this["LookFor"] = value; }
}
[ConfigurationProperty("SendTo")]
public String SendTo
{
get { return (string)this["SendTo"]; }
set { this["SendTo"] = value; }
}
}
{
public MyHandler() { }
[ConfigurationProperty("Rule")]
public RulesCollection Rule //把Rule映射为一个配置元素的集合类
{
get { return (RulesCollection)this["Rule"]; }
set { this["Rule"] = value; }
}
}
//此处只是重写了这个类的几个最基本的的方法
public class RulesCollection : ConfigurationElementCollection
{
public Rule this[int index]
{
get { return (Rule)BaseGet(index); }
set
{
if (BaseGet(index) != null) { BaseRemoveAt(index); }
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Rule();
}
protected override object GetElementKey(ConfigurationElement element)
{
return element.GetHashCode();
}
}
public class Rule : ConfigurationElement
{
public Rule() { }
[ConfigurationProperty("LookFor")]
public String LookFor
{
get { return (string)this["LookFor"]; }
set { this["LookFor"] = value; }
}
[ConfigurationProperty("SendTo")]
public String SendTo
{
get { return (string)this["SendTo"]; }
set { this["SendTo"] = value; }
}
}
使用:
MyConfigSectionHandler.MyHandler config =
(MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection("RewriteRules");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < config.Rule.Count; i++)
{
sb.Append("<h2>Attributes in the Rule Element:</h2>");
sb.AppendFormat("LookFor = {0}<br/>", config.Rule[i].LookFor.ToString());
sb.AppendFormat("SendTo = {0}<br/>", config.Rule[i].SendTo.ToString());
}
Response.Write(sb);
(MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection("RewriteRules");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < config.Rule.Count; i++)
{
sb.Append("<h2>Attributes in the Rule Element:</h2>");
sb.AppendFormat("LookFor = {0}<br/>", config.Rule[i].LookFor.ToString());
sb.AppendFormat("SendTo = {0}<br/>", config.Rule[i].SendTo.ToString());
}
Response.Write(sb);
最后,可以看到并不复杂的配置也写了如此多的代码。个人觉得仍然不是很方便,如果你有大量的配置并且与Web设置关系并不大,建议还是写到自定义的XML文件比较好。那样读写更方便一些。