1

My web config is as follows:

  <appSettings>
    <add key="One" value="1" xdt:Transform="Insert"/>
  </appSettings>

If I need to access the value 1, I can write the below code:

ConfigurationManager.AppSettings("One");

But if the web config is like,

  <countryAppSettings>
    <add key="Two" value="2" xdt:Transform="Insert"/>
  </countryAppSettings>

can I access the value in the following form, using a helper class for ConfigurationManager class

ConfigurationManager.CountryAppSettings("Two");

Is this possible in c#?

J.Joseph
  • 234
  • 2
  • 11
  • 1
    http://softwarebydefault.com/code-samples/extending-configurationmanager-get-values-by-type/ You might wan't to look at this post – MajkeloDev Nov 17 '15 at 11:52
  • @MajkeloDev It is not answering my question. – J.Joseph Nov 17 '15 at 11:58
  • @wudzik I don't believe this is a duplicate of that question, that is about attempting to access a custom section in a configuration file. I believe this is about creation of custom sections and how they can have a code representation more akin to https://msdn.microsoft.com/en-us/library/2tw134k3.aspx – Ian Nov 17 '15 at 12:04
  • @lan you are right. Thanks – J.Joseph Nov 17 '15 at 12:04
  • @Ian ok, my bad. I thought it's more about accessing custom section through `ConfigurationManager` – Kamil Budziewski Nov 17 '15 at 12:06
  • @wudzik I am not trying to access the section using GetSection() method. – J.Joseph Nov 17 '15 at 12:06
  • @J.Joseph I was writing up an answer, but to make it not a link only answer there's quite a bit of code/markup to write up before posting – Ian Nov 17 '15 at 12:06
  • @J.Joseph but this is an option to do so – Kamil Budziewski Nov 17 '15 at 12:06
  • @wudzik Yeah, I agree with you. But my primary goal is to create my custom method to ConfigurationManager class (static class), using a helper class. – J.Joseph Nov 17 '15 at 12:07

2 Answers2

0

Yes you can do this, although you do also need to include a custom config section in your configuration file. There's an MSDN article here describing how to do it.

So you'll need to add in your custom configuration section first:

<configuration>
  <configSections>
    <sectionGroup name="countrySettings">
      <section name="countrySetting" type="Custom.CountrySettingSection" allowLocation="true" allowDefinition="Everywhere" />
    </sectionGroup>
  </configSections>
</configuration>

You'd then typically define your settings more like this, which is great if you've got a really rich object:

<countrySetting customKey="2">
   <richerObject>2</richerObject>
</countrySetting>

You'd then need a backing object constructed like so:

namespace Custom
{
 public class CountrySettingSection : ConfigurationSection
    {
        // Create a "customKey" attribute.
        [ConfigurationProperty("customKey", DefaultValue = "0", IsRequired = false)]
        public int CustomKey
        {
            get
            { 
                return (int)this["customKey"]; 
            }
            set
            { 
                this["customKey"] = value; 
            }
        }
}
Ian
  • 33,605
  • 26
  • 118
  • 198
0
  1. You can't create that way. maybe should visit this link https://msdn.microsoft.com/en-us/library/bb383977.aspx
  2. ConfigurationManager.AppSetting is property ,no method. This property type is NameValueCollection you can use like this

    ConfigurationManager.AppSettings["One"] or ConfigurationManager.AppSettings.GetValues("One")

  3. You can customize your web.config https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

Fatih Sert
  • 151
  • 1
  • 4
  • For the most part, answers like this that rely on links only (especially ones in Turkish?) are discouraged. Answers on SO should be the stopping point for solution seekers and links have a tendency to break. If there is relevant info on the link please build an answer around that, and only include the links to cite your solution. – TayTay Nov 17 '15 at 13:08