zoukankan      html  css  js  c++  java
  • 自定义配置文件的读取

    自定义配置,配置在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中的值

  • 相关阅读:
    【每天都要看一下】
    【这里有别人的经验,也有好玩的发现】
    【WPF】Listbox模板内button点击选中当前listboxItem
    【WFP】弹出窗口不在win10 任务列表里显示的方法
    PSD路径转换为 WPF path 的data
    【WPF】Listbox内item的样式替换默认选中样式和鼠标滑过样式
    【WPF】ListBox1内嵌套ListBox2 2的滑轮滚动阻止1的滚动解决方法
    【C#】文本框拼音检索汉字
    【WPF】Datagrid显示最低下一跳
    【C#】绝对随机数
  • 原文地址:https://www.cnblogs.com/zhuyuchao/p/6179980.html
Copyright © 2011-2022 走看看