1

I'm trying to create a section in web config where I can have a list of names.

So to do that I have done the following:

public class MyCustomSection : ConfigurationSection
{
    public string Name
    {
        get
        {
            return (string) this["name"];
        }
        set
        {
            this["name"] = value;

        }
    }
}

In the web.config I have added the following:

<configSections>
    <sectionGroup name="myCustomSectionGroup">
      <section name="myCustomSection" type="MySite.MyCustomSection"/>
  </sectionGroup>
</configSections>

<myCustomSectionGroup>
  <myCustomSection name="MyFirstName"></myCustomSection>
  <myCustomSection name="MySecondName"></myCustomSection>
</myCustomSectionGroup>

According to the MS guide, this is how its done: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

The next part I need to do is grab (in a IEnumerable format, list, array, anything really) myCustomSection. I need to know all the entries. However, I can't seem to find how that is done anywhere. Looking at the ConfigurationManager.GetSection, it returns an Object, not a list or ConfigurationSectionGroup or ConfigurationSection.

How do I grab the values from my custom section and loop over them, lets say just output them in console, their names.

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • You need to specifically cast the object returned by `ConfigurationManager.GetSection` to `MyCustomSection` . – Chetan Feb 12 '18 at 17:18
  • @ChetanRanpariya There are multiple of MyCustomSection, how do I get them all in a list? – Bagzli Feb 12 '18 at 17:22
  • Does this not help you? https://stackoverflow.com/q/1189364/125981 – Mark Schultheiss Feb 12 '18 at 17:23
  • @MarkSchultheiss no I don't see how it answers my question. – Bagzli Feb 12 '18 at 17:26
  • 2
    I am not sure if you can have multiple sections with the same name. https://stackoverflow.com/questions/5661544/correct-implementation-of-a-custom-config-section-with-nested-co this might help you. – Chetan Feb 12 '18 at 17:26
  • Note here you see how someone extracted to an app.config (custom named) instead of adding to the web.config https://stackoverflow.com/a/46453247/125981 I know it is not a direct answer but it does get a simplified web.config perhaps making deployment easier. – Mark Schultheiss Feb 12 '18 at 18:14
  • @ChetanRanpariya Your article lead me to a solution. If you don't post an answer, I'll do it once SO lets me with an example of working code based on my question. However, if you do post it I'll mark yours as correct, but please do give example for future visitors. – Bagzli Feb 12 '18 at 18:26

1 Answers1

1

The full answer to my question can be found in the following SO Question:

Correct implementation of a custom config section with nested collections?

The problem is that I can't create multiple sections, but I can create multiple elements of that section via collection. With that I have to configure the classes to support my layout. Please see the SO Answer posted above for full example.

Bagzli
  • 6,254
  • 17
  • 80
  • 163