這篇文章主要介紹在web.config或者app.config中如何增加自定義配置節(jié),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
簡單鍵值對
web.config
<configSections> <section name="users" type="System.Configuration.NameValueSectionHandler"/> </configSections> <users configSource="users.config"></users>
users.config
<users> <add key="beijing" value="123"></add> <add key="tianjin" value="123"></add> </users>
c#
NameValueCollection users = System.Configuration.ConfigurationManager.GetSection("users") as NameValueCollection; Response.Write(users.Keys[0]+"</br>"+users.Keys[1]);
復(fù)雜類型
web.config
<configSections> <section name="roles" type="EBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI"/> </configSections> <roles configSource="roles.config"> </roles>
roles.config
<roles> <add username="beijing" password="123"></add> <add username="tianjin" password="123"></add> </roles>
RolesCofig.cs
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; } } }
c#
XmlNode roles= System.Configuration.ConfigurationManager.GetSection("roles") as XmlNode; Response.Write(roles.ChildNodes [0].Attributes["username"].InnerText);
還可以將配置節(jié)定義為一個(gè)實(shí)體
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);
以上是“在web.config或者app.config中如何增加自定義配置節(jié)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道!
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
本文名稱:在web.config或者app.config中如何增加自定義配置節(jié)-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://sd-ha.com/article44/dceihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制開發(fā)、網(wǎng)頁設(shè)計(jì)公司、微信小程序、微信公眾號、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容