ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合
核心代码
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
{
public class DefaultElement : ConfigurationElement
{
[ConfigurationProperty("factory")]
public string Factory
{
get
{
return this["factory"] as string;
}
set
{
this["factory"] = value;
}
}
} public class FactoryElement : ConfigurationElement
{
[ConfigurationProperty("name")]
public string Name
{
get
{
return this["name"] as string;
}
set
{
this["name"] = value;
}
} [ConfigurationProperty("assembly")]
public string Assembly
{
get
{
return this["assembly"] as string;
}
set
{
this["assembly"] = value;
}
} [ConfigurationProperty("class")]
public string Class
{
get
{
return this["class"] as string;
}
set
{
this["class"] = value;
}
}
} public class FactoryElements : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FactoryElement();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((FactoryElement)element).Name;
} public FactoryElement this[string name]
{
get
{
return BaseGet(name) as FactoryElement;
}
}
} public class DbFactorySection : ConfigurationSection
{
[ConfigurationProperty("default")]
public DefaultElement DefaultFactory
{
get
{
return this["default"] as DefaultElement;
}
set
{
this["default"] = value;
}
}
[ConfigurationProperty("factorys")]
public FactoryElements Factorys
{
get
{
return this["factorys"] as FactoryElements;
}
set
{
this["factorys"] = value;
}
}
} }
配置文件
<configSections>
<section name="dbFactory" type="Commons.DbFactorySection"/>
</configSections> <dbFactory >
<default factory="sql"></default>
<factorys>
<add name="sql" assembly="Hello.Data" class="SqlDbFactory" />
<add name="oracle" assembly="Hello.Data" class="OracleDbFactory" />
<add name="sqlite" assembly="Hello.sqlite" class="SqliteDbFactory" />
<!--还可以添加无数个<add 节点-->
</factorys>
</dbFactory>
代码中使用
DbFactorySection dfs = ConfigurationManager.GetSection("dbFactory") as DbFactorySection;
FactoryElements fes = dfs.Factorys;
FactoryElement feSql = fes["sql"];
FactoryElement feSqlite = fes["sqlite"]
ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合的更多相关文章
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节
主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...
- 使用 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变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...
随机推荐
- WinForm中WebBrowser的使用
最近由于工作需要,研究了下Winform的WebBrowser控件,在这里给大家分享下. 1.WebBrowser导航 WebBrowser在进行导航的时候需要用到Navigate(string ur ...
- Linux 常用命令 :ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...
- Texture Filter中的Bilinear、Trilinear以及Anistropic Filtering
1. 为什么在纹理采样时需要texture filter(纹理过滤)?我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture ...
- 用Redis构建分布式锁-RedLock(真分布)
在不同进程需要互斥地访问共享资源时,分布式锁是一种非常有用的技术手段. 有很多三方库和文章描述如何用Redis实现一个分布式锁管理器,但是这些库实现的方式差别很大,而且很多简单的实现其实只需采用稍微增 ...
- linux命令(6):rmdir 命令
rmdir命令 rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删除某目录时也必须具有对父目录的写 ...
- tcl学习
variables(变量) 语法:set varname value 例如:set a 5 注意:大小写敏感,任意长度,任意字符 使用之前无需申明 substitution(替换) 1 变量值替换 $ ...
- SegmentControl的多选项实现(标题栏)
NSArray *titleArr = @[STR(@"全部"), STR(@"未使用"), STR(@"已赠送"), STR(@" ...
- UiAutomator自动化测试框架介绍
UiAutomator自动化测试框架介绍 环境搭建 1 必要条件 1.1 JDK 1.2 SDK(API高于15) 1.3 Eclipse 2 ...
- [问题记录.VisualStudio]VS2013无法新增和打开项目
[问题描述] 1) 打开项目失败,报“项目文件只读”或“空引用”错误. 2) 无法新建项目,没有任何可用模板. 3) TFS都正常 [问题产生] 机器环境: 1) 装的Win10双系统,其中一个系统是 ...
- 在windows下面配置redis集群遇到的一些坑
最近工作不忙,就决定学习一下redis.因为一直在windows下工作,不会linux,没办法就选择在windows下配置redis. windows下配置redis集群的文章有很多,比如:http: ...