web.config中的自定义部分并获取所有项

时间:2018-02-12 17:13:26

标签: c# asp.net-mvc asp.net-mvc-5 web-config

我正在尝试在网络配置中创建一个部分,我可以在其中列出名称。

为此,我做了以下事情:

public class MyCustomSection : ConfigurationSection
{
    public string Name
    {
        get
        {
            return (string) this["name"];
        }
        set
        {
            this["name"] = value;

        }
    }
}

在web.config中我添加了以下内容:

<configSections>
    <sectionGroup name="myCustomSectionGroup">
      <section name="myCustomSection" type="MySite.MyCustomSection"/>
  </sectionGroup>
</configSections>

<myCustomSectionGroup>
  <myCustomSection name="MyFirstName"></myCustomSection>
  <myCustomSection name="MySecondName"></myCustomSection>
</myCustomSectionGroup>

根据MS指南,这就是它的完成方式:{​​{3}}

我需要做的下一部分是抓取(以IEnumerable格式,列表,数组,真正的任何东西)myCustomSection。我需要知道所有的条目。但是,我似乎无法找到在任何地方如何做到这一点。查看ConfigurationManager.GetSection,它会返回一个对象,而不是列表或ConfigurationSectionGroupConfigurationSection

如何从我的自定义部分获取值并循环遍历它们,让我们说只需在控制台中输出它们的名称。

1 个答案:

答案 0 :(得分:0)

我的问题的完整答案可以在以下SO问题中找到:

Correct implementation of a custom config section with nested collections?

问题在于我无法创建多个部分,但我可以通过集合创建该部分的多个元素。有了这个我必须配置类来支持我的布局。请参阅上面发布的SO答案以获取完整示例。

相关问题