Asp.Net webconfig中使用configSections的用法


  最近閑來無事,研究研究公司的框架,無意中打開了webconfig頁面,發現了一個我不認識的節點<configSections></configSections>,於是百度之,大致的了解了它的作用,還是蠻重要的!!!但是我居然不知道!!!這是最騷的,瞬間覺得自己還是太年輕了!!!好了,不BB了,言歸正傳了。

1、configSections有什么用

大家都知道,webconfig文件中不能隨意的添加節點,稍有不慎,瀏覽器就GG了,報錯了,玩完了,整個人都不好了,(當然僅限於配置文件,你如果在外部XML文件了定義節點,然后生成對象,那就是你想怎么定義就怎么定義)。

所以configSections節點就是干這個事的,讓你在webconfig中定義自想要定義的節點,當然肯定是要按照它指定的規則來!!!下面就來說configSection制定的規則。

 

2、為什么需要自定義節點

說完configSections作用(幫助我們按照它的規則創建一系列自定義節點),接下來說所為什么需要自定義節點?

為了增加應用程序的可移植性,通常網站需要配置一些自定義的節點,例如:文件上傳的路徑等,再深入的應用,可以定義工廠方法需要創建的類。

 

3、configSections的使用方法

  <configSections>
    <sectionGroup name="WebSiteConfig">
      <section name="dbProviders" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/>
      <section name="fileUploadPath" type="ZC.DBUtility.WebSiteInfoHandler,ZC.DBUtility"/>
    </sectionGroup>
  </configSections>

(1)、定義一個configSection配置節

(2)、然后定義sectionGroup節點,這個配置節相當於所有section配置節的命名空間。

(3)、最后定義section配置節,通過這個配置節設置自定義節點的名稱和處理configSection的一般處理程序   注意:這個處理程序必須繼承IConfigurationSectionHandler不要問我為什么,你知道為什么!!!

 

下面開始設置自定義節點

 <WebSiteConfig>
    <dbProviders>
      <add key="DBInstance" value="ConnString" type="sqlserver"/>
    </dbProviders>
    <fileUploadPath>
      <add key="path" value="#" />
      <add key="path1" value="#1" />
    </fileUploadPath>
  </WebSiteConfig>

自定義節點的定義規則要和上面的configSections的定義規則保持一致

 

最后編寫一般處理程序,按照一定的規則獲取我們的自定義節點的信息

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Xml;

namespace ZC.DBUtility
{
    public class WebSiteInfoHandler : IConfigurationSectionHandler
    {
        /// <summary>
        /// 返回自定義節點對象字典
        /// </summary>
        /// <param name="parent">父對象</param>
        /// <param name="configContext">配置上下文對象</param>
        /// <param name="section">節 XML 節點</param>
        /// <returns> 創建的節處理程序對象。</returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            Dictionary<string, ConfigEntity> config = new Dictionary<string, ConfigEntity>();
            foreach (XmlNode node in section) {
                string key = string.Empty, value = string.Empty, type = string.Empty;
                if (node.Attributes["key"] != null)
                    key = node.Attributes["key"].Value;
                if(node.Attributes["value"] != null)
                    value = node.Attributes["value"].Value;
                if (node.Attributes["type"] != null)
                    type = node.Attributes["type"].Value;
                config.Add(key, new ConfigEntity(value, type));
            }
            return config;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZC.DBUtility
{
    public class ConfigEntity
    {
        /// <summary>
        /// 自定義節點對象
        /// </summary>
        /// <param name="_value">節點的value值</param>
        /// <param name="_type">節點的type值</param>
        public ConfigEntity(string _value, string _type) 
        {
            this.Value = _value;
            this.Type = _type;
        }

        public string _value;
        public string _type;
        public string Value {
            get { return _value; }
            set { _value = value; }
        }
        public string Type {
            get { return _type; }
            set { _type = value; }
        }
    }
}

ok,做完這幾步我們可以開始愉快的使用自定義節點的信息了

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZC.DBUtility;

namespace Web.Ado
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, ConfigEntity> config = ConfigurationSettings.GetConfig("WebSiteConfig/dbProviders") as Dictionary<string, ConfigEntity>;
            if (config != null) {
                foreach (string key in config.Keys) {
                    ConfigEntity ce = config[key] as ConfigEntity;
                    Response.Write(ce.RetrieveFullName());
                }
            }
        }
    }
}

 ok,done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM