net 中web.config单一解决方法 (其他配置引入方式)
近期一个项目需要写许多的配置项,发现在单个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 [].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);
net 中web.config单一解决方法 (其他配置引入方式)的更多相关文章
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- IIS上虚拟目录下站点的web.config与根站点的web.config冲突解决方法
IIS7.5上在站点下部署虚拟目录,访问虚拟目录下的项目提示与父节点配置冲突.,节点与的<system.web>节点与主站点的<system.web>冲突解决方法: 在站点下的 ...
- C#中web.config文件详解
C#中web.config文件详解 一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NE ...
- Asp.net中web.config配置文件详解(一)
本文摘自Asp.net中web.config配置文件详解 web.config是一个XML文件,用来储存Asp.NET Web应用程序的配置信息,包括数据库连接字符.身份安全验证等,可以出现在Asp. ...
- Asp.net中web.config配置文件详解
Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中 ...
- SRX550路由器缓存满了无法在web页面操作解决方法
SRX550路由器缓存满了无法在web页面操作解决方法 首页出现下图为满的标志,我这个文档就是解决这中情况,让web页面可以操作的 1. 打开命令行,输入用户密码,进入路由器 注意:这里使用te ...
- (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...
- ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法
ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法 (2016-10-21 16:49:53) 今天在做一个查询的时候,报了一个"ORA-01652无法通过 ...
- Oracle中的 UPDATE FROM 解决方法
转:http://www.cnblogs.com/JasonLiao/archive/2009/12/23/1630895.html Oracle中的 UPDATE FROM 解决方法 在表的更新操作 ...
随机推荐
- Linux Set Command
1. set -e "Exit immediately if a simple command exits with a non-zero status." When this o ...
- java面向对象--内部类
将一个类定义在另一个类里面,里面的那个类称为内部类,与属性.方法等一样视作外部类的成员.内部类提供了更好的封装,不允许同包中的其他类访问该内部类. 内部类作为外部类的成员,同样可以被4个访问限定符修饰 ...
- android 本地数据库sqlite的封装
单机android sqlite数据库的实现,这个数据库可与程序一起生成在安装包中 一.下载sqlite3.exe文件 二.运行 cmd 转到sqlite3.exe 所在目录 运行 sqlite ...
- org.hibernate.LazyInitializationException...no session or session was closed
org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role:cn.its.oa ...
- reshape: from long to wide format(转)
This is to continue on the topic of using the melt/cast functions in reshape to convert between long ...
- ArrayList源码解读
在端午节这个节日里,有一个特殊的任务,我带着你一起揭开"ArrayList"的真面目.从成员变量.构造函数.主要方法三部分,对ArrayList有进一步的认识,希望能够帮助你. 一 ...
- MySQL的复制
1.复制概述1.1.复制解决的问题数据复制技术有以下一些特点:(1) 数据分布(2) 负载平衡(load balancing)(3) 备份(4) 高可用性(high avail ...
- hdu5803
hdu5803 题意 给出四个整数 A B C D,问有多少个四元组 (a, b, c, d) 使 a + c > b + d 且 a + d >= b + c ,0 <= a &l ...
- Vulkan Tutorial 17 Rendering and presentation
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Setup 这一章节会把之前的所有内容进行整合.我们将会编写drawFrame函数, ...
- 搭建vue开发环境及各种报错处理
1.安装node.js 参考教程:http://nodejs.cn/download/ 我的是windows 64位系统,以此为例: (1)打开 http://nodejs.cn/download/ ...