自定义WebConfig部分返回集合属性

时间:2012-01-01 01:12:24

标签: c# asp.net

我在访问我的pages元素的proprties集合属性“name”时遇到了问题。 Pages有一个具有属性的页面字段集合。有人可以查看我的代码并告诉我如何在每个页面上都有一个具有name属性的页面集合并访问它的值。目前我的代码只返回页面加载而没有任何错误,因此我不知道最新情况以及如何获取属性字段。

<configSections>
  <sectionGroup name="site" type="MyProject.Configuration.Site">
    <section name="pages" type="MyProject.Configuration.Pages"/>      
  </sectionGroup>  
</configSections>


<site>
  <pages>
    <page name="test">        
    </page>
  </pages>    
</site>

类:

public class Site : ConfigurationSection
{
    [ConfigurationProperty("pages")]
    [ConfigurationCollection(typeof(PageCollection), AddItemName="page")]
    public PageCollection Pages
    {
        get
        {
            return base["pages"] as PageCollection;
        }
    }
}

public class PageCollection : ConfigurationElementCollection
{

    public PageCollection()
    {
        PageElement element = (PageElement)CreateNewElement();
        BaseAdd(element); // doesn't work here does if i remove it
    }

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

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

    public PageElement this[int index]
    {
        get
        {
            return (PageElement)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

}

public class PageElement : ConfigurationElement
{
    public PageElement() { }

    [ConfigurationProperty("name", IsKey=true, IsRequired=true)]
    public string Name
    {
        get
        {
            return (string)base["name"];
        }
        set
        {
            base["name"] = value;
        }
    }
}

访问我的属性的代码:

Pages pageSettings = ConfigurationManager.GetSection("site/pages") as Pages;
lblPage.Text = pageSettings.Page[0].Name;

1 个答案:

答案 0 :(得分:1)

问题是你的section元素应该是site,而不是pages:

public class Site: ConfigurationSection
{
    [ConfigurationProperty("pages")]
    public PageCollection Page
    {
        get
        {
            return base["pages"] as PageCollection;
        }
    }
}

<强>更新

事实证明,有一些问题需要解决:

1)需要将web.config更改为section而不是sectiongroup,并且应删除pages元素:

  <configSections>
    <section name="site" type="MyProject.Configuration.Site, MyProject.Configuration">
    </section>
  </configSections>

2)需要修改Site类:

public class Site : ConfigurationSection
{
    [ConfigurationProperty("pages")]
    [ConfigurationCollection(typeof(PageCollection), AddItemName="page")]
    public PageCollection pages
    {
        get
        {
            return base["pages"] as PageCollection;
        }
    }
}

3)PageCollection构造函数需要删除其代码。

    public PageCollection()
    {
    }

4)name属性需要从PageCollection中删除,或者至少标记为不需要。

5)现在调用检索设置:

Site site = ConfigurationManager.GetSection("site") as Site;

我已对此进行了测试并验证了这些更改是否会在配置中成功读取。

相关问题