1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > net 中web.config一个配置文件解决方法 (其他配置文件引入方式)

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

时间:2020-01-10 00:59:40

相关推荐

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 [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);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。