近期一个项目需要写许多的配置项,发现在单个web.config里面写的话会很乱也难于查找

所以搜了一下解决了,记录下来

一、   webconfig提供了引入其他config的方式

<connectionStrings configSource="Configs\database.config" />

这个是连接字符串的配置你可以在database。config里面写很多链接字符串以备自己调用

database。config里面的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings><add name="SDbContext" connectionString="Server=.;Initial Catalog=Self;User ID=sa;Password=password" providerName="System.Data.SqlClient" /></connectionStrings>

<appSettings configSource="Configs\system.config" />

这个是键值对的方式存放代码如下:

<?xml version="1.0" encoding="utf-8"?>
<appSettings><!-- ================== 1:开发系统相关配置 ================== --><!-- 登陆提供者模式:Session、Cookie--><add key="LoginProvider" value="Cookie" /><!-- 启用系统日志--><add key="IsLog" value="true" /><!-- 数据库超时间--><add key="CommandTimeout" value="180" /><!--启用IP过滤 --><add key="IsIPFilter" value="false" /><!-- ================== 2:系统软件参数配置 ================== --><!-- 联系我们 --><add key="Contact" value="TE Software(Mobility)" /><!-- 软件名称 --><add key="SoftName" value="Sub Self" /><!-- 软件版本 --><add key="Version" value="1.0" /><!-- 设置就应用路径 --><add key="AppName" value="" /><!-- 设置就应用路径 --><add key="SqlGetBomList" value="" />
</appSettings>

以上两个是不需要特殊的配置的,放到configuration里面 configSections的下面这样就可以

二、下面介绍自定义配置节

<configSections><section name="users" type="System.Configuration.NameValueSectionHandler"/></configSections><users  configSource="users.config"></users>

注意configsections里面的一条,是声明这是以什么组织方式

users.config 里面的内容如下:

<users><add key="beijing" value="123"></add><add key="tianjin" value="123"></add>
</users>

获取配置的方式:

NameValueCollection users = System.Configuration.ConfigurationManager.GetSection("users") as NameValueCollection;//   Response.Write(users.Keys[0]+"</br>"+users.Keys[1]);
users.Get("beijing");

  三、复杂类型:

复杂类型的声明就不同了

<configSections><section name="roles" type="EBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI"/></configSections>
<roles configSource="roles.config"></roles>

内容如下

<roles><add username="beijing" password="123"></add><add username="tianjin" password="123"></add>
</roles>

获取方式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EBuy.Chapter3.NTier.WebUI
{public class RolesConfig : System.Configuration.IConfigurationSectionHandler{public object Create(object parent, object configContext, System.Xml.XmlNode section){return section;}}
}XmlNode roles= System.Configuration.ConfigurationManager.GetSection("roles") as XmlNode;Response.Write(roles.ChildNodes [0].Attributes["username"].InnerText);

还可以配置为实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EBuy.Chapter3.NTier.WebUI
{public class RolesConfig : System.Configuration.IConfigurationSectionHandler{public object Create(object parent, object configContext, System.Xml.XmlNode section){var list=new List<Role>();for(int i=0;i<section.ChildNodes.Count;i++){list.Add(new Role (){Username =section.ChildNodes[i].Attributes["username"].InnerText ,Password =section.ChildNodes[i].Attributes["password"].InnerText });}return list;}}public class Role{public string Username { get; set; }public string Password{get;set;}}
}var roles = System.Configuration.ConfigurationManager.GetSection("roles") as List<EBuy.Chapter3.NTier.WebUI.Role >;Response.Write(roles.First ().Username);

  

转载于:https://www.cnblogs.com/barnet/p/7132522.html

net 中web.config一个配置文件解决方法 (其他配置文件引入方式)相关推荐

  1. iconfont阿里图标引入不生效的解决方法,三种引入方式详细步骤。

    1.登录阿里图标官网 2.注册自己的账户,创建项目 3.加入自己使用的图标,加入购物车 4.选中项目 5.选中引入方式: 第一种.使用统一码 Unicode 第二种.使用class Font clas ...

  2. 在.net中读写config文件的各种方法(转载)

    阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...

  3. springmvc项目搭建过程中遇到的BUG及其解决方法

    需要导入对应的类/jar包 没有找到该类,说明没有编译成功, java.lang.ClassNotFoundException org.springframework.web.util.Log4jCo ...

  4. Nginx中worker connections问题的解决方法

    这篇文章主要介绍了Nginx中worker connections问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 查看日志,有一个[warn]: 3660#0: 20000 work ...

  5. android百度地图覆盖物异步加载图片,Android 百度地图marker中图片不显示的解决方法(推荐)...

    目的: 根据提供的多个经纬度,显示所在地的marker样式,如下: 问题: 1.发现marker中在线加载的图片无法显示出来: 2.获取多个对象后,却只显示出了一个marker: 以下为官网实现方法: ...

  6. 在.net中读写config文件的各种方法(转)

    在.net中读写config文件的各种方法阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CD ...

  7. 取出Cookie中的中文显示乱码解决方法。经验证第三种方法有效。

    取出Cookie中的中文显示乱码解决方法 HttpUtility.UrlEncode(Response.Cookies[ "newcookie "].Values);  HttpU ...

  8. 记flume部署过程中遇到的问题以及解决方法(持续更新)

    项目需求是将线上服务器生成的日志信息实时导入kafka,采用agent和collector分层传输,app的数据通过thrift传给agent,agent通过avro sink将数据发给collect ...

  9. 在VNC中Xfce4中Tab键失效的解决方法

    博客新址: http://blog.xuezhisd.top 邮箱:xuezhisd@126.com 说明 在Ubuntu Server 14.04上安装了xfce4桌面环境,但是却发现***在终端中 ...

最新文章

  1. .NET Core类库项目中如何读取appsettings.json中的配置
  2. 2.3 词嵌入的特性-深度学习第五课《序列模型》-Stanford吴恩达教授
  3. java 二维链表_Java数据结构与算法----数组与链表
  4. 51nod 1402 最大值 3级算法题 排序后修改限制点 时间复杂度O(m^2)
  5. 支付宝支付-手机浏览器H5支付
  6. pycharm 远程调试图文_pycharm远程调试openstack的图文教程
  7. 斩断亏损,让利润奔跑
  8. python socket 实现的简单http服务器
  9. Vue, element-ui Module build failed: Error: No PostCSS Config found
  10. C/C++语言经典、实用、趣味程序设计编程百例精解(2)
  11. RSTP比STP快在哪?
  12. HSRP在企业网中的应用(CISCO小凡模拟器)
  13. 路畅畅云固件升级教程_【图】E-Link功能/智能语音 路畅畅云+车载导航评测_汽配中国网...
  14. TPS、RPS和QPS是什么
  15. C/C++ 代码分析警告集
  16. 金融相关计算机面试题,银行及其他金融企业笔试和面试经验
  17. R报错:caret包与InformationValue包在绘制confusionmatrix时遇到的问题
  18. Linux命令操作——常用解压缩命令
  19. HTTP请求和相应格式
  20. 静态磨损衡和动态磨损平衡

热门文章

  1. dev控件swiftplot图滚动方法_无限轮播图使用Scroller就这么简单
  2. Git初学札记(三)————创建Git版本库
  3. python threading-单线程 多线程 主线程 子线程 setDeamon join
  4. Java反射 Class类
  5. python实现火车票查询_python实现12306火车票查询器
  6. HTML+CSS+JS实现 ❤️年年有鱼祝福背景特效❤️
  7. android wifi热点项目总结,高通Android wifi移植和wifi热点问题总结
  8. linux setarch 命令,6.22. Util-linux-2.23.1
  9. java线程卡住排查_基于 Java 线程栈 排查问题
  10. 为什么计算机关机慢,电脑关机慢是什么原因 电脑关机慢的原因【图文】