开发者

Access ConfigurationSection from ConfigurationElement

开发者 https://www.devze.com 2022-12-31 12:33 出处:网络
I have a configuration class that maps web.config, something like that: public class SiteConfigurationSection : ConfigurationSection

I have a configuration class that maps web.config, something like that:

 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;
            }
        }      

    开发者_Python百科}

Meaning if connection string name value is missing in Module section in web.config file, the value should be read from configurationsection.

How to do that?


This would depend on the name of your section's tag.

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

精彩评论

暂无评论...
验证码 换一张
取 消