web.config C#中使用自定義配置


web.config C#中使用自定義配置

<configuration>
    <configSections>     //配置節聲明區域,包含配置節和命名空間聲明
        <section>               //配置節聲明
            <sectionGroup/>        //定義配置節組
        </section>        //配置節組中的配置節聲明
    </configSections>
    <appSettings/> //預定義配置節
    <Custom element for configuration section>   //配置節設置區域
</configuration>

 

 

自定義配置可分為:自定義配置節和自定義配置節組。

 

1. 自定義配置節

要使用自定義配置需要修改兩處地方,一是在<configSections/>中聲明配置節,二是在<Custom element for configuration section>處設置配置節的具體配置。

聲明配置節語法:

 

 

<section name="" type=""/>

 

<section>:聲明新配置節

name:自定義配置節的名稱

type: 自定義配置節的類型,主要包括System.Configuration.SingleTagSectionHandler、 System.Configuration.DictionarySectionHandler、 System.Configuration.NameValueSectionHandler。

SingleTagSectionHandler 配置節返回類型為 Systems.Collections.IDictionary
DictionarySectionHandler 配置節返回類型為 Systems.Collections.IDictionary
NameValueSectionHandler 配置節返回類型為 Systems.Collections.Specialized.NameValueCollection

 

下面是一個完整的自定義配置節示例代碼:

 

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="UrlString" type="System.Configuration.SingleTagSectionHandler"/>
    <section name="UrlString2" type="System.Configuration.DictionarySectionHandler"/>
  </configSections>
  <UrlString action="add" paramString="id=1"></UrlString>
  <UrlString2>
    <add key="add" value="id=1"/>
    <add key="edit" value="id=2"/>
  </UrlString2>
</configuration>

 

程序中讀取配置代碼如下:

 

        static void Main(string[] args)
         {
            //訪問 UrlString 配置節
             IDictionary dict = ConfigurationManager.GetSection("UrlString") as IDictionary;
             Console.WriteLine(string.Format("{0}?{1}", dict["action"], dict["paramString"]));

            //訪問 UrlString2 配置節
             IDictionary dict2 = ConfigurationManager.GetSection("UrlString2") as IDictionary;
            foreach (DictionaryEntry e in dict2)
             {
                 Console.WriteLine(string.Format("{0}?{1}", e.Key, e.Value));
             }

             Console.Read();
         }

 

 

 

2. 自定義配置節組

配置節組是使用<sectionGroup>元素來聲明,在配置節組中可以包括多個配置節<section>,如下:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="TestGroup">
            <section name="Test" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>
    
    <TestGroup>
        <Test>
            <add key="Hello" value="World"/>
        </Test>
    </TestGroup>
</configuration>

 

下面是訪問這個配置節組的代碼:

 

NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //輸出Hello World

 

 

 

 

3. 通過 IConfigurationSectionHandler 接口實現自定義配置處理類

IConfigurationSectionHandler 接口原型:

 

public interface IConfigurationSectionHandler
{
     Object Create(Object parent, Object configContext, XmlNode section)
}

 

Create 方法必須可由多個線程同時調用,即要保證線程安全。

 

示例代碼:

 

    public class UrlString : IConfigurationSectionHandler
     {

        private string _action;

        public string Action
         {
            get { return _action; }
            set { _action = value; }
         }

        private string _param;

        public string Param
         {
            get { return _param; }
            set { _param = value; }
         }

        #region IConfigurationSectionHandler 成員

        public object Create(object parent, object configContext, XmlNode section)
         {
             Hashtable hashtable = new Hashtable();
            foreach (XmlAttribute attribute in section.Attributes)
             {
                 hashtable[attribute.Name] = attribute.Value;
             }
            return hashtable;
         }

        #endregion

        public static UrlString Create()
         {
             UrlString us = new UrlString();
             Hashtable ht = ConfigurationManager.GetSection("UrlString") as Hashtable;
             us.Action = (string)ht["action"];
             us.Param = (string)ht["paramString"];
            return us;
         }
     }


免責聲明!

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



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