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());

 

转载于:https://www.cnblogs.com/yonsy/p/5620563.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值