使用 ConfigurationSection 創建自定義配置節


我們可以通過用自己的 XML 配置元素來擴展標准的 ASP.NET 配置設置集, 要完成這一功能,我們必須實現繼承System.Configuration.ConfigurationSection 類來實現自定義配置節,在1.0中當然也可以通過IconfigurationSectionHandler 接口創建自定義配置節!這里我們主要學一下通過ConfigurationSection類來實現簡單的配置處理程序.
      先看一下在web.config文件中的配置情況,在這里有兩個元素,第一個mysection,有兩個屬性user,password,第二個也有兩個屬性element1,和element2。配置比較簡單。
   <!-- ////////////////////////////////////////////////////////////////////////////////////////////// -->
  
< configSections >
    
< sectionGroup  name ="mygroup" >
      
< section  name ="mysection"
                       type
="ConfigSection"
                        allowDefinition
="Everywhere"
                         allowLocation
="true" />
    
</ sectionGroup >
  
</ configSections >
  
<!-- ////////////////////////////////////////////////////////////////////////////////////////////// -->

  
< mygroup >
    
< mysection   user ="用戶"  password ="密碼" >
      
< element  element1 ="屬性1"  element2 ="屬性2" ></ element >
    
</ mysection >
  
</ mygroup >

        理解配置文件結構后,我們就需要用繼承自System.Configuration.ConfigurationSection的基類來實現簡單的配置類ConfigSection,在2.0中,我們只需要這一個類就能實現完成配置,下面請看代碼:
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 的摘要說明
///   </summary>
public   class  ConfigSection:ConfigurationSection
{
    
public  ConfigSection()
    {
        
//
        
//  TODO: 在此處添加構造函數邏輯
        
//
    }
[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 " ]; }
    }


}



    通過下面的代碼就可以獲得在配置文件中設置的值了
ConfigSection config  =  (ConfigSection)ConfigurationManager.GetSection( " mygroup/mysection " );
        Response.Write(
" 用戶名: " + config.User.ToString()  +   " 密碼: "   +  config.PassWord.ToString()  +   " 元素屬性: "   +  config.Element.Element1.ToString()  +  config.Element.Element2.ToString());


免責聲明!

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



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