自定义配置文件的读取

自定义配置,配置在Web.config 中的configSections节点中

<configSections>
<section name ="baidu" type="Test.Models.BaiduSection,Test" requirePermission="false"/>
</configSections>

  

  

然后在configSections节点下面添加

  <baidu configSource="baidu.config"/>

  

注意:section节点的name值必须与configSections节点下面添加的节点名相同。section接点type:Test.Models.BaiduSection是自定义配置类的完全限定名,Test是程序集名称。

baidu.config中的内容:

<?xml version="1.0" encoding="utf-8"?>
<baidu webSiteName="百度" webSiteUrl ="www.baidu.com">
  <menus>
    <add code="1" showName="网页" controllerName="wy" actionName="Index"/>
    <add code="2" showName="新闻" controllerName="ny" actionName="Index"/>
    <add code="3" showName="贴吧" controllerName="tb"  actionName="Index"/>
  </menus>
</baidu>

  

  

 

第一步:引用程序集System.Configuration

第二步:添加相应的类

首先添加MenuElement类

namespace Test.Models
{
    public class MenuElement : ConfigurationElement
    {
        [ConfigurationProperty("code", IsRequired = true, IsKey = true)]
        public int Code
        {
            get
            {
                return (int)this["code"];
            }
            set
            {
                this["code"] = value;
            }
        }
        [ConfigurationProperty("showName", IsRequired = true)]
        public string ShowName
        {
            get
            {
                return this["showName"] as string;
            }
            set
            {
                this["showName"] = value;
            }
        }
        [ConfigurationProperty("controllerName", IsRequired = true)]
        public string ControllerName
        {
            get
            {
                return this["controllerName"] as string;
            }
            set
            {
                this["controllerName"] = value;
            }
        }
        [ConfigurationProperty("actionName", IsRequired = true)]
        public string ActionName
        {
            get
            {
                return this["actionName"] as string;
            }
            set
            {
                this["actionName"] = value;
            }
        }

    }
}

  

  添加MenuElementCollection类

namespace Test.Models
{
    public class MenuElementCollection:ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MenuElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as MenuElement).Code;
        }
    }
}

  

  添加BaiduSection类

namespace Test.Models
{
    public class BaiduSection:ConfigurationSection
    {
        [ConfigurationProperty("webSiteName", IsRequired = true)]
        public string WebSiteName
        {
            get
            {
                return this["webSiteName"] as string;
            }
            set
            {
                 this["webSiteName"] = value;
            }
             
        }
        [ConfigurationProperty("webSiteUrl", IsRequired = true)]
        public string WebSiteUrl
        {
            get
            {
                return this["webSiteUrl"] as string;
            }
            set
            {
                this["webSiteUrl"] = value;
            }

        }
        [ConfigurationProperty("menus", IsRequired = true)]
        public MenuElementCollection Menus
        {
            get
            {
                return this["menus"] as MenuElementCollection;
            }
            set
            {
                this["menus"] = value;
            }

        }
    }
}

  

  

  第三步调用:

  BaiduSection baiduSection = (BaiduSection)ConfigurationManager.GetSection("baidu");

  就会得到对应自定义config中的值

posted @ 2016-12-14 16:51  576  阅读(288)  评论(0编辑  收藏  举报