2010-05-19 56 views
1

我有一個映射的web.config配置類,這樣的事情:訪問的ConfigurationSection自ConfigurationElement

public class SiteConfigurationSection : ConfigurationSection 
    { 
     [ConfigurationProperty("defaultConnectionStringName", DefaultValue = "LocalSqlServer")] 
     public string DefaultConnectionStringName 
     { 
      get { return (string)base["defaultConnectionStringName"]; } 
      set { base["defaultConnectionStringName"] = value; } 
     } 

     [ConfigurationProperty("Module", IsRequired = true)] 
     public ModuleElement Module 
     { 
      get { return (ModuleElement)base["Module"]; } 
     } 
    } 

    public class ModuleElement : ConfigurationElement 
    { 
     [ConfigurationProperty("connectionStringName")] 
     public string ConnectionStringName 
     { 
      get { return (string)base["connectionStringName"]; } 
      set { base["connectionStringName"] = value; } 
     } 

     public string ConnectionString 
     { 
      get 
      { 
       string str; 
       if (string.IsNullOrEmpty(this.ConnectionStringName)) 
       { 
        str =//GET THE DefaultConnectionStringName from SiteConfigurationSection; 
       } 
       else 
        str = this.ConnectionStringName; 

       return WebConfigurationManager.ConnectionStrings[str].ConnectionString; 
      } 
     }  

    } 

含義,如果連接字符串名稱值在web.config文件模塊部分缺失,值應爲從配置部分讀取。

如何做到這一點?

回答

0

這將取決於您的部分標籤的名稱。

var cs = 
    ((SiteConfigurationSection)WebConfigurationManager 
     .GetSection("mySectionTag")).DefaultConnectionString; 
+0

這是不是你是誰,我問 – shivesh 2010-05-19 12:42:07

+0

@shivesh吧,我誤解你的問題。 – 2010-05-19 12:43:47

+0

@shivesh - 我最後的評論是爲了表明我修改了與您的問題相關的答案。請查閱。 – 2010-05-22 05:47:04

相關問題