繁体   English   中英

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

[英]Custom web.config sections (ASP.NET)

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

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

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

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

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

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

}

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

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

我认为您需要创建一个使用ConfigurationCollectionAttribute属性修饰的属性。 我扫描了你的代码,并没有在任何地方看到它的引用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM