2010-06-19 76 views
6

为相对较长的帖子提前道歉 - 我尽力提供尽可能多的相关信息(包括代码清单)!自定义web.config部分(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 存放模块的对象的集合

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

回答

3

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

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

+0

啊,非常好。非常感谢你,先生!工作过一种享受。第一次呢! – AndyBursh 2010-06-19 12:44:28