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中的值

  • 相关阅读:
    Linux内核空间-用户空间通信之debugfs
    Mysql 启动失败 报错 1067
    [置顶] hdu3018解题报告--也是白话几笔画学习总结
    【Todo】蒙特卡洛(蒙特卡罗)树 & 卷积网络
    基本分类方法——KNN(K近邻)算法
    SVM(支持向量机)与统计机器学习 & 也说一下KNN算法
    可重入锁 & 自旋锁 & Java里的AtomicReference和CAS操作 & Linux mutex不可重入
    【Todo】Nginx架构学习
    【转载】C++异常机制的学习
    关于协程的学习 & 线程栈默认10M
  • 原文地址:https://www.cnblogs.com/zhuyuchao/p/6179980.html
Copyright © 2011-2022 走看看