自定义WebConfig
来源:互联网 发布:搜达足球数据 编辑:程序博客网 时间:2024/05/21 09:50
在系统开发过程中,经常遇到添加一下配置信息的需求,有时候需要在App.config或Web.config中添加,如果永远都配置在appSetting节点下面,配置项多了,就显得比较混乱,其实我们也可以自定义节点配置。
首先,在configSection节点中添加自定义的配置信息,如下,其中Type中用于解释该配置信息的类与程序集名称。如下:
<configSections><section name="dataSourceSettings" type="XXX.App.ServicePlanEmailPromotion.DataSourceSettings,XXX.App.ServicePlanEmailPromotion"/></configSections>
然后就可以在WebConfig下添加自己定义的节点信息,跟定义xml文件格式一样,可自由定义。如下:
<dataSourceSettings dataBase="TempTable"><tables><add name="DM_ESPOR_SonicWall_Result_CS_B2B" type="CS" /><add name="DM_ESPOR_SonicWall_Result_EM_B2B" type="EM" /><add name="DM_ESPOR_SonicWall_Result_CS_B2C" type="CS"/><add name="DM_ESPOR_SonicWall_Result_EM_B2C" type="EM"/></tables></dataSourceSettings>
Webconfig中需要的节点配置信息定义好之后,就需要建立对应的Class来读取这些信息,也就是上面提到的 XXX.App.ServicePlanEmailPromotion.DataSourceSettings.
创建的Class内容如下:
public sealed class DataSourceSettings : ConfigurationSection { [ConfigurationProperty("tables", IsRequired = true)] public TableCollection Tables { get { return (TableCollection)base["tables"]; } } [ConfigurationProperty("dataBase", IsRequired = true)] public string DataBaseName { get { return (string)base["dataBase"]; } } }
其中Tables和DataBaseName两个属性分别对应配置文件中tables和dataBase的内容。
TableCollection是一个集合类,如下:
public sealed class TableCollection : ConfigurationElementCollection { private IList<DataSourceTable> tableList; protected override ConfigurationElement CreateNewElement() { return new TableElement(); } protected override object GetElementKey(ConfigurationElement element) { TableElement tableElement = element as TableElement; return tableElement.Name; } public IList<DataSourceTable> TableList { get { if (this.tableList == null) { tableList = new List<DataSourceTable>(); foreach (TableElement table in this) { tableList.Add(new DataSourceTable { Name = table.Name, Type = table.Type }); } } return this.tableList; } } public new string this[string name] { get { string type = string.Empty; if (this.tableList.Where(table => table.Name == name).Any()) { return this.tableList.Where(table => table.Name == name).FirstOrDefault().Type; } else { throw new ArgumentNullException("Not found " + name + " in tables nodes."); } } } }
public sealed class TableElement : ConfigurationElement { /// <summary> /// Table type. /// </summary> [ConfigurationProperty("type", IsRequired = true)] public string Type { get { return (string)base["type"]; } set { base["type"] = value; } } /// <summary> /// Table name. /// </summary> [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string)base["name"]; } set { base["name"] = value; } } }
public class DataSourceTable { public string Name { get; set; } public string Type { get; set; } }
那么该如何使用呢? 请看下方:
DataSourceSettings dataSource = ConfigurationManager.GetSection("dataSourceSettings") as DataSourceSettings; var dataSourceList = dataSource.Tables.TableList;
0 0
- 自定义WebConfig
- 自定义webconfig节点
- WEBconfig 自定义配置节(.net)
- webconfig 中的 configSections 自定义节点
- WebConfig.
- webconfig
- webconfig
- webconfig
- webconfig
- WebConfig
- webconfig
- webconfig
- WEBconfig 自定义配置节(.net) (转)
- 怎么读取webconfig和配置自定义节点?
- c# webconfig的自定义的使用方法
- c#中webconfig的自定义方法
- asp.net 在webconfig里自定义错误页
- 两个webconfig
- 奇虎360与腾讯不正当竞争案判决书
- CentOS 6.5 安装私有gitlab环境
- Ironjs
- bootstrap搜索图标的使用
- 网络相关知识点整理
- 自定义WebConfig
- Windows下,Eclipse的Android NDK(r8e) 配置
- git 版本控制系统
- 十一.2职员有薪水了
- JS拖拽元素 兼容IE
- oracle 最有用的将分隔符字符串转换成数组
- Nginx Location配置总结及基础最佳实践
- hdu1222
- linux内核sk_buff的结构分析