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变量记录会员的登录名,然后在购买商品页面通过判断会员是否登录确定其能否购买商品. ...
随机推荐
- jquery 获取form下的所有元素
<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <title> ...
- AngularJS作出简单聊天机器人
简单聊天机器人 很初级的对话框形式.以前做对话框使用js,今天尝试使用AngularJS做出来 这里直接使用自己写的JSON数据. <!DOCTYPE html> <html lan ...
- Unknown column 'a.root_id' in 'where clause'解决方法
最近遇到一个问题, update sy_user as a inner join sy_user_charge_log as b on b.username=a.username set recon ...
- DirectX12 Samples 学习笔记 – PredicationQueries
一.效果 这是一个比较简单的sample,运行sample可以看到,当红橙色长方形完全覆盖白色正方形时,白色正方形不显示,其他情况,均显示白色正方形. 二.实现 Render主要由三个部分组成 1.F ...
- 编程风格(Coding Style)要求
编程风格(Coding Style)要求2.1.1 文件(1) 每个模块(module)一般应存在于单独的源文件中,通常源文件名与所包含模块名相同.(2) 每个设计文件开头应包含如下注释内容:? 年份 ...
- 如何得到EF(ADO.NET Entity Framework)查询生成的SQL? ToTraceString Database.Log
ADO.NET Entity Framework ToTraceString //输出单条查询 DbContext.Database.Log //这里有详细的日志
- 历史命令:history
[root@linux ~]# history [n][root@linux ~]# history [-c][root@linux ~]# history [-raw] histfiles参数:n ...
- modelsim 中 WAVE窗口中能不能只显示变量名,而不显示路径
可以的,在wave窗口左下角有一个黑色的logo,你点击它就可以省电路径,只显示port名称,再点击就切换回来了,如图红色圈圈标记的logo,你可以试试!
- poj 1384 Piggy-Bank(完全背包)
Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10830 Accepted: 5275 Descr ...
- JSON Object(如NSDictionary,NSArray)转化为JSON格式的NSString #iOS开发
NSString *string = [self jsonObjectToJSONString:inputDataDic]; -(NSString*)jsonObjectToJSONString:(i ...