主要代码,一定要继续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"]; }
} }
}

配置文件

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

ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节的更多相关文章

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

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

  2. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

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

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

  4. 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串

      1.在<connectionStrings> 标签里添加连接 <connectionStrings> <add name="ConnectionName&q ...

  5. asp.net项目中通过Web.config配置文件及文件夹的访问权限!

    描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面 ...

  6. IOC注入框架——Unity中Web.Config文件的配置与调用

    Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...

  7. Unity中Web.Config文件的配置与调用

    在上一篇文章“Unit简单依赖注入”我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即当我们不想使用MP3 ...

  8. web.config文件中配置数据库连接的两种方式

    web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报    分类: 数据库(74)  在网站开发 ...

  9. 如何在web.config文件中配置Session变量的生命周期

    实例说明:在网上购物商城中,为了维护在线购物环境,一般只有注册会员才可以购买商品.实现购物功能时,先通过Session变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...

随机推荐

  1. java命名规范有感

    一. 注()里的内容是自己的吐槽 二. 命名规约 不能以下划线或美元符开始和结尾.反例:_name,$name,name_,name$.(我就从来没这样用过) 不能使用拼音和英文组合方式命名.更不能以 ...

  2. oracle 批量插入-支持序列自增

    1.创建表.序列 -- Create table create table test_batch ( id number not null, name ), account ) ) -- Create ...

  3. centos7安装openoffice

    [摘要:间接登录openoffice民网下载硬件包,但跳转的页里却不停挨没有开.末了只能正在末端里干活下了: 1. 挑选得当的版本:http://www.openoffice.org/download ...

  4. jquery指index

    $(selector).index(element) 获得元素相对于选择器的 index 位置.<ul>    <li><a href="#nogo" ...

  5. 【Python全栈笔记】07 [模块二] 20 Oct 冒泡排序

    给出一个列表,进行冒泡排序 原理算法: li = [52, 37, 23, 11, 3, 1, ] print(li) # 每次循环,进行一次排序,列表内数字两两比较,最大的数字排到最末尾 # 一共循 ...

  6. tfs 任务自定义项

    vs 命令 cd C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies 导出工作项类型文件 修改xml ...

  7. android:ems的作用

    android:ems用来设置EditText或TextView显示的字符宽度. 比如:android:ems="10" 设置TextView或EditText为10个字符的宽度, ...

  8. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  9. 使用JS脚本获取url中的参数

    第一种方式:使用分隔符及循环查找function getQueryString(name) { // 如果链接没有参数,或者链接中不存在我们要获取的参数,直接返回空 if(location.href. ...

  10. Java的二维数组的应用及杨辉三角的编写

    (1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...