自定义web.config部分(ASP.NET)

And*_*unt 6 c# asp.net web-config

为相对较长的帖子提前道歉 - 我试图提供尽可能多的相关信息(包括代码清单)!

我一直在为web.config文件中的一个自定义部分实现一些我过去几个小时工作的东西,但我似乎无法让它工作.以下是我想用作XML结构的内容:

<mvcmodules>
    <module moduleAlias="name" type="of.type">
        <properties>
            <property name="propname" value="propvalue" />
        </properties>
    </module>
</mvcmodules>
Run Code Online (Sandbox Code Playgroud)

目前,我有以下类设置和工作(有点):

  • ModuleSection
  • ModuleCollection
  • ModulePropertyCollection
  • ModuleProperty

我能看到接近我想要做的方式的唯一方法是将我的声明包装在另一个父调用中.但是,当我这样做时,如果我有多个标签实例("该元素可能只在本节中出现一次."),并且使用一个标签,则信息未被读入对象.

我写了一些基本文档,这样你就可以了解我是如何构建这个文档的,并希望看到我出错的地方

ModuleSection 此类包含ModulesCollection对象

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

ModulesCollection 包含Module对象的集合

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Module 包含有关模块和ModulePropertyCollection对象的信息

    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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ModulePropertyCollection 包含ModuleProperty对象的集合

    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

ModuleProperty 保存有关模块属性的信息

    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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Qui*_*ith 3

我认为您需要创建一个用ConfigurationCollectionAttribute属性装饰的属性。我扫描了您的代码,但没有在任何地方看到对其的引用。

MSDN 链接实际上有一个有用的示例。我最近自己整理了一个自定义配置部分,发现该页面完全足够了。请参阅我的问题,了解如何在 Visual Studio 中为您的配置部分启用 Intellisense 支持。