自定义配置节处理程序

本文详细介绍了在.Net2.0中如何通过继承ConfigurationSection类来自定义配置节,包括简单的配置示例和一个复杂的URL重写配置示例。通过这些示例,读者可以了解如何创建和使用自定义配置节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.Net 2.0 中的自定义配置节的处理程序有了点变化,不用再实现System.Configuration.IConfigurationSectionHandler接口,而是要继承 System.Configuration.ConfigurationSection 类。先来看个简单点的实现:
在web.config中,有如下的配置:

None.gif<configSections>    
None.gif        
<section name="CustomerProviders" type="MyConfigSectionHandler.MyHandler" /> 
None.gif
</configSections>
None.gif
<CustomerProviders>
None.gif        
<Provider Name="QingProvider" File="FileName" />
None.gif
</CustomerProviders>
None.gif

很简单,只有一个属性,此处关键是把相应的属性对应好.

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

这一切做好之后,使用就非常简单了

MyConfigSectionHandler.MyHandler config =
             (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection(
"CustomerProviders");
Response.Write(config.Provider.Name 
+ "<br>"  + config.Provider.File);

下面来看一个复杂点的,比如我们要写一个URL Rewrite的配置。在web.config中做了这样的配置:

None.gif<configSections>
None.gif      
<section  name="RewriteRules" type="MyConfigSectionHandler.MyHandler"  />
None.gif
</configSections>
None.gif
None.gif
<RewriteRules>
None.gif    
<Rule>
None.gif       
<add  LookFor="~/Software.aspx" SendTo="Catagory.aspx?CategoryID=1 "/>
None.gif        
<add  LookFor="~/Hardware.aspx" SendTo="Catagory.aspx?CategoryID=2 "/>
None.gif        
<add  LookFor="~/Services.aspx" SendTo="Catagory.aspx?CategoryID=3 "/>
None.gif      
</Rule>
None.gif
</RewriteRules>None.gifNone.gif

None.gifpublic class MyHandler : ConfigurationSection
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public MyHandler() dot.gif{ }
InBlock.gif
InBlock.gif        [ConfigurationProperty(
"Rule")]
InBlock.gif        
public RulesCollection Rule        //把Rule映射为一个配置元素的集合类
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (RulesCollection)this["Rule"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis["Rule"= value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

None.gif
   //此处只是重写了这个类的几个最基本的的方法
None.gif
public class RulesCollection : ConfigurationElementCollection  
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif        
public Rule this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (Rule)BaseGet(index); }
InBlock.gif               
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (BaseGet(index) != nulldot.gif{  BaseRemoveAt(index);  }
InBlock.gif                   BaseAdd(index, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
protected override ConfigurationElement CreateNewElement()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Rule();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override object GetElementKey(ConfigurationElement element)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return element.GetHashCode(); 
       
             }

ExpandedBlockEnd.gif    }

None.gif
None.gif
public class Rule : ConfigurationElement
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Rule() dot.gif{ }       
InBlock.gif
InBlock.gif        [ConfigurationProperty(
"LookFor")]
InBlock.gif        
public String LookFor
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get  dot.gifreturn (string)this["LookFor"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set  dot.gifthis["LookFor"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [ConfigurationProperty(
"SendTo")]
InBlock.gif        
public String SendTo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get  dot.gifreturn (string)this["SendTo"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set  dot.gifthis["SendTo"= value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

使用:

None.gif MyConfigSectionHandler.MyHandler config =
None.gif            (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection(
"RewriteRules");
None.gif
None.gif        System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
None.gif
None.gif        
for (int i = 0; i < config.Rule.Count; i++)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            sb.Append(
"<h2>Attributes in the Rule Element:</h2>");
InBlock.gif            sb.AppendFormat(
"LookFor = {0}<br/>", config.Rule[i].LookFor.ToString());
InBlock.gif            sb.AppendFormat(
"SendTo = {0}<br/>", config.Rule[i].SendTo.ToString());
ExpandedBlockEnd.gif        }

None.gif        Response.Write(sb);
None.gif

最后,可以看到并不复杂的配置也写了如此多的代码。个人觉得仍然不是很方便,如果你有大量的配置并且与Web设置关系并不大,建议还是写到自定义的XML文件比较好。那样读写更方便一些。

转载于:https://www.cnblogs.com/qing/archive/2006/07/24/458567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值