• ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节


    主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    namespace Commons
    {
        /// <summary>
        /// ConfigSection 的摘要说明
        /// </summary>
        public class MyConfigSection : ConfigurationSection
        {
            public MyConfigSection()
            { 
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }
            [ConfigurationProperty("user", DefaultValue = "admin", IsRequired = true)]
            public string User
            {
                get { return (string)this["user"]; }
                set { this["user"] = value; }
            }
    
            [ConfigurationProperty("password", DefaultValue = "password", IsRequired = true)]
            public string PassWord
            {
                get { return (string)this["password"]; }
                set { this["password"] = value; }
            }
    
            [ConfigurationProperty("element")]
            public elementinfo Element
            {
                get { return (elementinfo)this["element"]; }
                set { this["element"] = value; }
            }
        }
        public class elementinfo : ConfigurationElement
        {
            public elementinfo() { }
    
    
            [ConfigurationProperty("element1", DefaultValue = "element1", IsRequired = true)]
            public string Element1
            {
                get { return (string)this["element1"]; }
            }
    
            [ConfigurationProperty("element2", DefaultValue = "element2", IsRequired = true)]
            public string Element2
            {
                get { return (string)this["element2"]; }
            }
    
    
        }
    }
    View Code

    配置文件

    <configSections>
        <sectionGroup name="mygroup">
          <section name="mysection" type="Commons.MyConfigSection" allowDefinition="Everywhere" allowLocation="true"/>
        </sectionGroup>
      </configSections>

    <mygroup> <mysection user="用户" password="密码"> <element element1="属性1" element2="属性2"></element> </mysection> </mygroup>

    代码中使用值

     Commons.MyConfigSection config = (Commons.MyConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
     Response.Write("用户名:" + config.User.ToString() + "密码:" + config.PassWord.ToString() + "元素属性:" + config.Element.Element1.ToString() + config.Element.Element2.ToString());
  • 相关阅读:
    Ubuntu-14.04-QT开发环境搭建-(一)
    解决使用Qt creator时出现Cannot overwrite file ..Permission denied
    Github上关于大数据的开源项目、论文等合集
    Qt5.4中遇到找不到头文件<QApplication>等。
    qt的下载地址
    完整的qt安装教程
    Ubuntu14.04安装Matlab2014a
    Ubuntu14.04安装搜狗输入法的一点小问题
    把OnDraw和OnPaint弄清楚(转贴)
    Ubuntu上挂载源代码,docker容器中共享这个原代码,实现自动部署
  • 原文地址:https://www.cnblogs.com/yonsy/p/5620563.html
Copyright © 2020-2023  润新知