【问题标题】:Custom web.config sections (ASP.NET)自定义 web.config 部分 (ASP.NET)
【发布时间】:2010-06-19 12:19:36
【问题描述】:

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

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

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

目前,我设置了以下课程并开始工作(有点):

  • 模块部分
  • 模块集合
  • 模块
  • ModulePropertyCollection
  • 模块属性

我能看到的接近我想要这样做的方式的唯一方法是将我的声明包装在另一个名为 .但是,当我这样做时,如果我有多个标签实例(“该元素在本节中只能出现一次。”),我会收到错误消息,并且使用一个标签,信息不会被读入对象。

我已经编写了一些基本文档,因此您可以了解我是如何构建它的,并希望看到我哪里出错了

模块部分 这个类拥有一个 ModulesCollection 对象

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

模块集合 持有 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;
    }
}

}

模块 包含有关模块和 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;
    }
}

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

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

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

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

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

    【讨论】:

    • 啊,很好。非常感谢您,好先生!工作了一个款待。也是第一次!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多