Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom web.config sections (ASP.NET)

Apologies in advance for the relatively long post - I've tried to provide as much relevant information as I can (including code listings)!

I've been working on implementing a custom section in the web.config file for a little something I'm working for the past few hours, but I can't seem to get it working. The following is what I'd like to use as my XML structure:

<mvcmodules>
    <module moduleAlias="name" type="of.type">
        <properties>
            <property name="propname" value="propvalue" />
        </properties>
    </module>
</mvcmodules>

Currently, I have the following classes set up and working (somewhat):

  • ModuleSection
  • ModuleCollection
  • Module
  • ModulePropertyCollection
  • ModuleProperty

The only way I can see to get close to the way I want to do this is to wrap my declarations in another parent called . However, when I do this, I get an error if I have more than one instance of the tag ("The element may only appear once in this section."), and with one tag, the information is not being read in to the objects.

I've written a little basic documentation so you can get an idea of how I'm structuring this and hopefully see where I'm going wrong

ModuleSection This class holds a ModulesCollection object

namespace ASPNETMVCMODULES.Configuration
{
    public class ModulesSection : System.Configuration.ConfigurationSection
    {
        [ConfigurationProperty("modules", IsRequired = true)]
    public ModuleCollection Modules
    {
        get
        {
            return this["modules"] as ModuleCollection;
        }
    }
}

ModulesCollection Holds a collection of Module objects

namespace ASPNETMVCMODULES.Configuration
{
public class ModuleCollection : ConfigurationElementCollection
{
    [ConfigurationProperty("module")]
    public Module this[int index]
    {
        get
        {
            return base.BaseGet(index) as Module;
        }
        set
        {
            if (base.BaseGet(index) != null)
            {
                base.BaseRemoveAt(index);
            }

            this.BaseAdd(index, value);
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new Module();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Module)element).ModuleAlias;
    }
}

}

Module Contains information about the module and a ModulePropertyCollection object

    public class Module : ConfigurationElement
{
    [ConfigurationProperty("moduleAlias", IsRequired = true)]
    public string ModuleAlias
    {
        get
        {
            return this["moduleAlias"] as string;
        }
    }

    [ConfigurationProperty("type", IsRequired = true)]
    public string ModuleType
    {
        get
        {
            return this["type"] as string;
        }
    }

    [ConfigurationProperty("properties")]
    public ModulePropertyCollection ModuleProperties
    {
        get
        {
            return this["properties"] as ModulePropertyCollection;
        }
    }
}

ModulePropertyCollection Contains a collection of ModuleProperty objects

    public class ModulePropertyCollection : ConfigurationElementCollection
{
    public ModuleProperty this[int index]
    {
        get
        {
            return base.BaseGet(index) as ModuleProperty;
        }
        set
        {
            if (base.BaseGet(index) != null)
            {
                base.BaseRemoveAt(index);
            }

            this.BaseAdd(index, value);
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ModuleProperty();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ModuleProperty)element).Name;
    }
}

ModuleProperty Holds information about the module properties

    public class ModuleProperty : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get
        {
            return this["name"] as string;
        }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public string Value
    {
        get
        {
            return this["value"] as string;
        }
    }
}
like image 292
Andy Hunt Avatar asked Jun 19 '10 12:06

Andy Hunt


1 Answers

I think you need to create a property decorated with the ConfigurationCollectionAttribute attribute. I scanned your code and didn't see a reference to it anywhere.

The MSDN link for once actually has a useful example. I recently put together a custom config section myself, and found that page entirely adequate. See my question for how to enable Intellisense support for your config section in Visual Studio.

like image 82
Quick Joe Smith Avatar answered Sep 30 '22 13:09

Quick Joe Smith