我们可以通过用自己的 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());

使用 ConfigurationSection 创建自定义配置节的更多相关文章

  1. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

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

    核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...

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

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

  4. C#如何使用和开发自定义配置节

    在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...

  5. C#创建自定义配置节点

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

  6. C# 创建自定义配置节点1

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

  7. 迁移appseting.json创建自定义配置中心

    创建一个自定义的配置中心,将框架中各类配置,迁移至数据库,支持切换数据库,热重载. 说在前面的话 自使用.net Core框架以来,配置大多存在json文件中: [框架默认加载配置]文件为appset ...

  8. C# App.config 自定义 配置节

    1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration>  ...

  9. .NET:自定义配置节

    背景 对于编译型应用程序来说,参数化程序行为是非常有必要的,.NET有其标准的配置方法,我们可以可以扩展. 示例 代码 using System; using System.Collections; ...

随机推荐

  1. 动态扩展php组件(mbstring为例)

    1.进入源码包中的mbstring目录 cd ~/php-/ext/mbstring/ 2.启动phpize /usr/local/php/bin/phpize 3.配置configure ./con ...

  2. fmri 分析数据 fsl & spm 两大平台比对

    基于下面这份ppt:Comparing SPM and FSL, by lChris Rorden fsl & spm都是免费的,都很受欢迎.spm更受欢迎. 两者的区别在于何时利用norma ...

  3. OpenLayers2中的事件_以Popup为例

    SATURDAY, 21 MARCH 1-Preface 前几天阅读学习了OpenLayers'Cookbook中的第四章——Working with events. 从AFDS系统的开发项目进行至今 ...

  4. Eclipse中执行maven命令

    1.如下图,右击需要执行maven命令的工程,选择"Debug As"或"Run As",再选择"Maven build..." 进行如上操 ...

  5. webService 三要素

    WebService(jax-ws)三要素 SOAP: 基于HTTP协议,采用XML格式,用来传递信息的格式. WSDL: 用来描述如何访问具体的服务.(相当于说明书) UDDI: 用户自己可以按UD ...

  6. [转]Sql server 大数据量分页存储过程效率测试附代码

    本文转自:http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html 在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下 ...

  7. Storm常见模式——分布式RPC

    Storm常见模式——分布式RPC 本文翻译自:https://github.com/nathanmarz/storm/wiki/Distributed-RPC,作为学习Storm DRPC的资料,转 ...

  8. OTL调用存储过程/函数及注意事项

    OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个 C++ 编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库. OTL  ...

  9. android 开源项目集合

    http://p.codekk.com/ http://www.apkbus.com/code.php http://androidxref.com/ https://www.androidos.ne ...

  10. Java笔记8:Hibernate连接Oracle

    1下载hibernate-3.6.0 Final.zip到任意目录,解压缩后得到hibernate目录 2下载slf4j-1.7.13.zip到任意目录,解压缩后得到slf4j-1.7.13 3操作数 ...