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

6

抱歉,本文相对较长 - 我尽量提供了尽可能多的相关信息(包括代码列表)!

我一直在努力实现一个自定义部分的web.config文件,为我过去几个小时所做的一些小事情提供帮助,但似乎无法使其正常工作。以下是我想要用作XML结构的内容:

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

目前,我已经设置并且有以下类(有点)工作:

  • ModuleSection
  • ModuleCollection
  • Module
  • 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;
        }
    }
}

ModulesCollection 保存一组模块对象

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;
    }
}

模块 包含有关模块的信息和一个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;
        }
    }
}

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;
    }
}

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;
        }
    }
}
1个回答

3
我认为您需要创建一个使用ConfigurationCollectionAttribute属性装饰的属性。我检查了您的代码,没有在任何地方看到它的参考。
MSDN链接实际上有一个有用的例子。最近我自己编写了一个自定义配置段,并发现该页面完全足够。请参见我的问题,了解如何在Visual Studio中为配置部分启用智能感知支持。

啊,非常好。非常感谢您,先生!一招奏效。而且还是第一次尝试! - Andy Hunt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接