Is there a way to access the value of a custom configuration section using a key, as in AppSettings?

I have a custom configuration section in my web.config as follows:

<configSection> <section name="CustomConfig" type="ConfigSectionRoot" allowLocation="true" allowDefinition="Everywhere"/> </configSection> <CustomConfig> <ConfigRoot> <add key="DataBase" value="CouchDB"/> <add key="FrontEnd" value="Asp.Net"/> <add key="AppName" value="Virtual WorkPlace"/> </ConfigRoot> </CustomConfig> <AppSettings> <add key="DataBase" value="CouchDB"/> </AppSettings> 

My ConfigSectionRoot.cs looks like this:

 public class ConfigSectionRoot:ConfigurationSection { [ConfigurationProperty("key", DefaultValue = "", IsKey = true, IsRequired = true)] public string Key { get { return ((string)(base["key"])); } set { base["key"] = value; } } [ConfigurationProperty("value", DefaultValue = "", IsKey = false, IsRequired = false)] public string Value { get { return ((string)(base["value"])); } set { base["value"] = value; } } } 

If I use AppSettings instead of Custom Config, I could access it, for example:

 string results= ConfigurationManager.AppSettings["Database"]; // results wil contain "CouchDB" 

Is there a way to achieve the same in the Customized Config section ??? Pls help me

+4
source share
2 answers

NameValueSectionHandler

If your configuration should not be larger than the key store, I would choose NameValueSectionHandler .

 <section name="customConfig" type="System.Configuration.NameValueSectionHandler"/> <!-- ... --> <customConfig> <add key="DataBase" value="CouchDB" /> <add key="FrontEnd" value="Asp.Net" /> <add key="AppName" value="Virtual WorkPlace" /> </customConfig> 

Then you can read it, like appSettings:

 var customConfig = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("customConfig");//i have changed like this and it worked fine var database = customConfig["DataBase"]; 

SingleTagSectionHandler

You can also achieve the same with SingleTagSection:

 <section name="customConfig" type="System.Configuration.SingleTagSectionHandler"/> <!-- ... --> <customConfig database="CouchDB" frontEnd="Asp.Net" appName="Virtual Workplace" /> 

And then query it with:

 var customConfig = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetConfig("customConfig"); var database = customConfig["database"]; 
+10
source

The .NET configuration structure provides the ConfigurationElementCollection class for representing lists of items. In the above example, the implementation of the ConfigurationElementCollection is represented by the xml ConfigRoot element. The collection must have children of type "ConfigSectionRoot". The ConfigSectionRoot class must inherit from ConfigurationElement, not the Configuration section.

You must create a separate class representing the XMLConfig element. This class is the root of your configuration and should inherit from ConfigurationSection.

 public class CustomConfigConfigurationSection : ConfigurationSection { public static CustomConfigConfigurationSection Section { get { return ConfigurationManager.GetSection("customConfig") as CustomConfigConfigurationSection; } } public ConfigConfigurationElementCollection ConfigRoot { get { return this["configRoot"] as ConfigConfigurationElementCollection; } } } public class ConfigConfigurationElement : ConfigurationElement { [ConfigurationProperty("key")] public string Key { get { return (string)this["key"]; } } [ConfigurationProperty("value")] public string Value { get { return (string)this["value"]; } } } public class ConfigConfigurationElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ConfigConfigurationElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((ConfigConfigurationElement)element).Key; } // Slight hack to look up the direct value property of the ConfigConfigurationElement from the collection indexer public new string this[string key] { get { return (base[key] as ConfigConfigurationElement).Value;//I m getting the error in this line } } } 

Direct use:

 var section = CustomConfigConfigurationSection.Section; var value = section.ConfigRoot["key"]; 
0
source

Source: https://habr.com/ru/post/1439344/


All Articles