2010-06-19 58 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