使用 ConfigurationSection 創(chuàng)建自定義配置節(jié)
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
我們可以通過(guò)用自己的 XML 配置元素來(lái)擴(kuò)展標(biāo)準(zhǔn)的 ASP.NET 配置設(shè)置集,要完成這一功能,我們必須實(shí)現(xiàn)繼承System.Configuration.ConfigurationSection 類來(lái)實(shí)現(xiàn)自定義配置節(jié),在1.0中當(dāng)然也可以通過(guò)IconfigurationSectionHandler 接口創(chuàng)建自定義配置節(jié)!這里我們主要學(xué)一下通過(guò)ConfigurationSection類來(lái)實(shí)現(xiàn)簡(jiǎn)單的配置處理程序。
先看一下在web.config文件中的配置情況,在這里有兩個(gè)元素,第一個(gè)mysection,有兩個(gè)屬性u(píng)ser、password,第二個(gè)也有兩個(gè)屬性element1和element2,配置比較簡(jiǎn)單。 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 理解配置文件結(jié)構(gòu)后,我們就需要用繼承自System.Configuration.ConfigurationSection的基類來(lái)實(shí)現(xiàn)簡(jiǎn)單的配置類ConfigSection,在2.0中,我們只需要這一個(gè)類就能實(shí)現(xiàn)完成配置,下面請(qǐng)看代碼: using System;
using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// ConfigSection 的摘要說(shuō)明 /// </summary> public class ConfigSection:ConfigurationSection { public ConfigSection() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } [ConfigurationProperty("user",DefaultValue="yanghong",IsRequired=true)] public string User { get { return (string)this["user"]; } set { this["user"] = value; } } [ConfigurationProperty("password",DefaultValue="password",IsRequired=true)] public string PassWord { get { return (string)this["password"]; } set { this["password"] = value; } } [ConfigurationProperty("element")] public elementinfo Element { get { return (elementinfo)this["element"]; } set {this["element"] = value; } } } public class elementinfo : ConfigurationElement { public elementinfo() { } [ConfigurationProperty("element1", DefaultValue = "element1", IsRequired = true)] public string Element1 { get { return (string)this["element1"]; } } [ConfigurationProperty("element2",DefaultValue="element2",IsRequired=true)] public string Element2 { get { return (string)this["element2"]; } } } 通過(guò)下面的代碼就可以獲得在配置文件中設(shè)置的值了: ![]() ![]() 該文章在 2021/5/11 18:42:19 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |