2010-08-15 117 views
0

我在我的web.config问题创建自定义配置节

<mySectionGroup> 

     <sectionOneSection> 
      <page path="~/123.aspx"></page> 
      <page path="~/456.aspx"></page> 
     </sectionOneSection> 
    </mySectionGroup> 

以下XML和下面的代码

public class SectionOneSection : ConfigurationSection { 

    [ConfigurationProperty("sectionOne")] 
    public PageConfigurationCollection Pages { 

     get { 
      return this["sectionOne"] as PageConfigurationCollection; 
     } 
    } 

    public static SectionOneSection GetConfig() { 

     return ConfigurationManager.GetSection("mySectionGroup/sectionOneSection") as 
       SectionOneSection; 
    } 
} 

public class PageElement : ConfigurationElement { 

    [ConfigurationProperty("path", IsRequired = true)] 
    public string Path { 

     get { 

      return this["path"].ToString(); 
     } 

     set { 
      this["path"] = value; 
     } 
    } 
} 

public class PageConfigurationCollection : ConfigurationElementCollection { 

    public PageElement this[int index] { 

     get { 

      return base.BaseGet(index) as PageElement; 
     } 

     set { 

      if (base.BaseGet(index) != null) { 
       base.BaseRemoveAt(index); 
      } 
      this.BaseAdd(index, value); 
     } 
    } 

    protected override string ElementName { 
     get { 
      return base.ElementName; 
     } 
    } 

    protected override ConfigurationElement CreateNewElement() { 
     return new PageElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) { 
     return ((PageElement)element).Path; 
    } 
} 

我也得到尝试检索时出现以下错误该部分

无法识别的元素“页面”。 (C:\ app \ web.config第39行)

我在这里丢失了什么?

回答

0

我怀疑它会用“增加”,而不是“页”工作:

<mySectionGroup> 
    <sectionOneSection> 
     <add path="~/123.aspx"/> 
     <add path="~/456.aspx"/> 
    </sectionOneSection> 
</mySectionGroup> 

你必须做一些额外的工作来定制“加法”的名称。至少,您需要覆盖AddElementName。如果记忆服务的话,还有其他的东西 - 我只是不记得具体细节。我认为这可能是你必须改变收集类型(覆盖CollectionType)。