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"]; }
} }
}
配置文件
<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创建自定义配置节的更多相关文章
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合
核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...
- 使用 ConfigurationSection 创建自定义配置节
我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...
- C#创建自定义配置节
在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
1.在<connectionStrings> 标签里添加连接 <connectionStrings> <add name="ConnectionName&q ...
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面 ...
- IOC注入框架——Unity中Web.Config文件的配置与调用
Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...
- Unity中Web.Config文件的配置与调用
在上一篇文章“Unit简单依赖注入”我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即当我们不想使用MP3 ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- 如何在web.config文件中配置Session变量的生命周期
实例说明:在网上购物商城中,为了维护在线购物环境,一般只有注册会员才可以购买商品.实现购物功能时,先通过Session变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...
随机推荐
- Android笔记之——线程
Java: package com.example.test_ui_thread01;import android.support.v7.app.AppCompatActivity;import an ...
- 关于Thomas Brinkhoff移动对象生成器的修改
关于地图数据的写出 控制地图路径数据的输出 修改routing.Edge.java 路径写出源码 public void write (EntryWriter out) { out.print(id) ...
- nw_socket_handle_socket_event解决
http://www.bkjia.com/IOSjc/1158465.html 出现问题如下 to a parent directory scheduled for deletion nw_endpo ...
- 微信小程序 wx.getUserInfo 解密 C# 代码
花了6小时,弄出来的代码.网上的是PHP代码 public static string DecodeUserInfo(string raw, string signature,string encry ...
- Facade(外观)-对象结构型模式
1.意图 为子系统中的一组接口提供一个一致的接口,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 2.动机 将一个系统划分成若干子系统有利于降低系统的复杂性.一个常见的设计目 ...
- Gevent协程
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...
- PHP-redis中文文档介绍(转自http://www.jb51.net/article/33887.htm)
Redis::__construct构造函数$redis = new Redis(); connect, open 链接redis服务参数host: string,服务地址port: int,端口号t ...
- Android Studio 简介及导入 jar 包和第三方开源库方[转]
原文:http://blog.sina.com.cn/s/blog_693301190102v6au.html Android Studio 简介 几天前的晚上突然又想使用 Android Studi ...
- mysql主从不一致解决方法
方法一:忽略错误后,继续同步 该方法适用于主从库数据相差不大,或者要求数据可以不完全统一的情况,数据要求不严格的情况 stop slave; #表示跳过一步错误,后面的数字可变 set global ...
- windows7安装python2.7及scrapy
http://www.cnblogs.com/txw1958/archive/2012/07/12/scrapy_installation_introduce.html 第0步:当然是安装python ...