LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

使用 ConfigurationSection 创建自定义配置节

admin
2021年5月11日 18:40 本文热度 3842
我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来实现自定义配置节,在1.0中当然也可以通过IconfigurationSectionHandler 接口创建自定义配置节!这里我们主要学一下通过ConfigurationSection类来实现简单的配置处理程序。
先看一下在web.config文件中的配置情况,在这里有两个元素,第一个mysection,有两个属性user、password,第二个也有两个属性element1和element2,配置比较简单。
  <!--//////////////////////////////////////////////////////////////////////////////////////////////-->
  
<configSections>
    
<sectionGroup name="mygroup">
      
<section name="mysection"
                       type
="ConfigSection"
                        allowDefinition
="Everywhere"
                         allowLocation
="true"/>
    
</sectionGroup>
  
</configSections>
  
<!--//////////////////////////////////////////////////////////////////////////////////////////////-->

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

理解配置文件结构后,我们就需要用继承自System.Configuration.ConfigurationSection的基类来实现简单的配置类ConfigSection,在2.0中,我们只需要这一个类就能实现完成配置,下面请看代码:
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;

/// <summary>
/// ConfigSection 的摘要说明
/// </summary>
public class ConfigSection:ConfigurationSection
{
    
public ConfigSection()
    {
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }
[ConfigurationProperty(
"user",DefaultValue="yanghong",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"]; }
    }
}

通过下面的代码就可以获得在配置文件中设置的值了:
ConfigSection config = (ConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
        Response.Write(
"用户名:"+config.User.ToString() + "密码:" + config.PassWord.ToString() + "元素属性:" + config.Element.Element1.ToString() + config.Element.Element2.ToString());

该文章在 2021/5/11 18:42:19 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved