.net中自定义配置文件节点

无论是做asp.net开发还是做winform开发都离不开配置文件,但是很明显这些配置信息都是.net内部设定好的,如果你想自己添加节点怎么办呢?这就是今天我们探讨的主要问题,我们通过继承ConfigurationSection类来使用在配置文件中的自定义节点。好了,下面我们一步一步来做吧。

第一步:在配置文件中添加配置信息,注意包括两个部分。

<?xml version="1.0"?> <configuration> <configSections> <!--下面是自己添加--> <sectionGroup name="MyGroup"> <section name="MySection" type="MyConfigurationSection.RewriterConfigSection, MyConfigurationSection"/> </sectionGroup> </configSections> <!--下面是自己添加--> <MyGroup> <MySection> <RewriterRule name="node1" lookFor="~/Products/Foot/.aspx" sendTo="~/TestPage/ListProductsByCategory.aspx?CategoryID=1" /> <RewriterRule name="node2" lookFor="~/Products/Drink/.aspx" sendTo="~/TestPage/ListProductsByCategory.aspx?CategoryID=2" /> </MySection> </MyGroup> </configuration>

配置文件中无关的部分已经去掉了,注意首先在configSections几点钟配置自定义节点信息然后添加相应的自定义节点信息。

第二步:依据配置节点信息书写相应的类。

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; namespace MyConfigurationSection { public class RewriterConfigSection:ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public RewriterRuleElementCollection MySection { get { return (RewriterRuleElementCollection)base[""]; } } } public class RewriterRuleElement:ConfigurationElement { [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string)base["name"]; } set { base["name"] = value; } } [ConfigurationProperty("lookFor", IsRequired = true)] public string LookFor { get { return (string)base["lookFor"]; } } [ConfigurationProperty("sendTo", IsRequired = true)] public string SendTo { get { return (string)base["sendTo"]; } } } public class RewriterRuleElementCollection : ConfigurationElementCollection { public RewriterRuleElementCollection() { RewriterRuleElement rrElement = CreateNewElement() as RewriterRuleElement; BaseAdd(rrElement); } protected override ConfigurationElement CreateNewElement() { return new RewriterRuleElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((RewriterRuleElement)element).Name; } protected override string ElementName { get { return "RewriterRule"; } } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } public RewriterRuleElement this[int index] { get { return (RewriterRuleElement)BaseGet(index); } } public new RewriterRuleElement this[string name] { get { return (RewriterRuleElement)BaseGet(name); } } } }

为了方便观看,几个类都写到了同一个文件中。

第三步:访问我们的自定义节点信息。

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyConfigurationSection { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { RewriterConfigSection section = (RewriterConfigSection)System.Configuration.ConfigurationManager.GetSection("MyGroup/MySection"); lbInfo.Text = section.MySection["node2"].LookFor; } } }

够简单了吧,我们可以随心所欲的访问节点信息了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值