locked
HOW TO set my own settings in Web.config RRS feed

  • Question

  • User1266736465 posted

    Hi, Guys

    I am trying to accomplish the following using the web.config and my own name conventional settings,

    for instance (I would like to add the web.config the following)

    <MySettings>
        <add group="store1" value="Bronx" />
        <add group="store2" value="Brooklyn" />
        <add group="store3" value="Manhattan" />
    </MySettings>

    I would like to know how read the above mentioned and store them into an array or <lists> for validation purpose

    if array has the value equal to the store location found in database table then allow it. (data retrieval is no problem)

    Could the above mentioned be possible and could you please provide c# sample code on how to.

    I know how to read the value from the web.config using the ConfigurationManager.AppSettings[SettingName] but in the case above I am trying to use my own tags;

    hope the above mentioned is understandable.

    Thanks

    Thursday, November 2, 2017 6:53 PM

Answers

  • User2103319870 posted

    <MySettings>
        <add group="store1" value="Bronx" />
        <add group="store2" value="Brooklyn" />
        <add group="store3" value="Manhattan" />
    </MySettings>

    I would like to know how read the above mentioned and store them into an array or <lists> for validation purpose

    You can read the values from Custom Sections in WebConfig like below

    Add the below 3 classes to your solution. Please note the class is designed as per the provided element names if you change that then the code wont work.

    MySettingsElement.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace AspNetWebApp
    {
        public class MySettingsElement : ConfigurationSection
        {
            [ConfigurationProperty("group", IsRequired = true,IsKey =true)]
            public string Group
            {
                get { return this["group"] as string; }
            }
    
            [ConfigurationProperty("value")]
            public string Value
            {
                get { return (string)this["value"]; }
            }
        }
    }

    MySettingsElementCollection.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace AspNetWebApp
    {
        [ConfigurationCollection(typeof(MySettingsElement))]
        public class MySettingsElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new MySettingsElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((MySettingsElement)element).Group;
            }
        }
    }

    MySettingsConfigSection.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace AspNetWebApp
    {
        public class MySettingsConfigSection : ConfigurationSection
        {
            [ConfigurationProperty("MySettings", IsDefaultCollection = true)]
            public MySettingsElementCollection MySettings
            {
                get { return (MySettingsElementCollection)this["MySettings"]; }
                set { this["MySettings"] = value; }
            }
        }
    }

    NOw modify your webconfig to add configSection

    <configuration>
      <configSections>
        <section name="MySettingsConfig" type="AspNetWebApp.MySettingsConfigSection" />
      </configSections>
      </configuration

    make sure the type value is a fully qualified name

    next change your custom config settings like below

    <MySettingsConfig> 
        <MySettings> 
          <add group="store1" value="Bronx" />
          <add group="store2" value="Brooklyn" />
          <add group="store3" value="Manhattan" />
        </MySettings>
      </MySettingsConfig>

    now you can read the value like below

      MySettingsConfigSection _Config = ConfigurationManager.GetSection("MySettingsConfig") as MySettingsConfigSection;
    
                foreach (MySettingsElement item in _Config.MySettings)
                {
                    string group = item.Group;
                    string value = item.Value;
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 2, 2017 8:54 PM

All replies

  • User753101303 posted

    Hi,
    See https://msdn.microsoft.com/en-us/library/2tw134k3.aspx  to see how to define a custom configuration section. If you don't plan further changes, it seems overkill to me. For now I would just use :

    <appSettings>
        <add key="store" value="Bronx,Brooklyn,Manhattan" />
    </appSettings>

    and would expose this as an array using String.Split.

    Thursday, November 2, 2017 8:36 PM
  • User2103319870 posted

    <MySettings>
        <add group="store1" value="Bronx" />
        <add group="store2" value="Brooklyn" />
        <add group="store3" value="Manhattan" />
    </MySettings>

    I would like to know how read the above mentioned and store them into an array or <lists> for validation purpose

    You can read the values from Custom Sections in WebConfig like below

    Add the below 3 classes to your solution. Please note the class is designed as per the provided element names if you change that then the code wont work.

    MySettingsElement.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace AspNetWebApp
    {
        public class MySettingsElement : ConfigurationSection
        {
            [ConfigurationProperty("group", IsRequired = true,IsKey =true)]
            public string Group
            {
                get { return this["group"] as string; }
            }
    
            [ConfigurationProperty("value")]
            public string Value
            {
                get { return (string)this["value"]; }
            }
        }
    }

    MySettingsElementCollection.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace AspNetWebApp
    {
        [ConfigurationCollection(typeof(MySettingsElement))]
        public class MySettingsElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new MySettingsElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((MySettingsElement)element).Group;
            }
        }
    }

    MySettingsConfigSection.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace AspNetWebApp
    {
        public class MySettingsConfigSection : ConfigurationSection
        {
            [ConfigurationProperty("MySettings", IsDefaultCollection = true)]
            public MySettingsElementCollection MySettings
            {
                get { return (MySettingsElementCollection)this["MySettings"]; }
                set { this["MySettings"] = value; }
            }
        }
    }

    NOw modify your webconfig to add configSection

    <configuration>
      <configSections>
        <section name="MySettingsConfig" type="AspNetWebApp.MySettingsConfigSection" />
      </configSections>
      </configuration

    make sure the type value is a fully qualified name

    next change your custom config settings like below

    <MySettingsConfig> 
        <MySettings> 
          <add group="store1" value="Bronx" />
          <add group="store2" value="Brooklyn" />
          <add group="store3" value="Manhattan" />
        </MySettings>
      </MySettingsConfig>

    now you can read the value like below

      MySettingsConfigSection _Config = ConfigurationManager.GetSection("MySettingsConfig") as MySettingsConfigSection;
    
                foreach (MySettingsElement item in _Config.MySettings)
                {
                    string group = item.Group;
                    string value = item.Value;
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 2, 2017 8:54 PM
  • User1266736465 posted

    This is great, I thought that it could not possible or I should say it was not going to be that easy, I've been trying and all I was getting errors and wrong tag in web.config

    I am going to try your way, again thank you very, very much for your help and sample code.

    Al

    Thursday, November 2, 2017 10:12 PM