net 中web.config一个配置文件解决方法 (其他配置文件引入方式)
生活随笔
收集整理的這篇文章主要介紹了
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);
轉載于:https://www.cnblogs.com/barnet/p/7132522.html
總結
以上是生活随笔為你收集整理的net 中web.config一个配置文件解决方法 (其他配置文件引入方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL备份和还原数据库及慢查询日志使
- 下一篇: jeesite在eclipse中部署