web.config 中的自定义部分并获取所有项目
Custom section in web.config and getting all items
我正在尝试在 Web 配置中创建一个部分,我可以在其中获得名称列表。
为此,我做了以下工作:
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 指南,它是这样完成的:https://msdn.microsoft.com/en-us/library/2tw134k3.aspx
我需要做的下一部分是获取(以 IEnumerable 格式、列表、数组,任何真正的格式)myCustomSection。我需要知道所有条目。但是,我似乎无法在任何地方找到它是如何完成的。查看 ConfigurationManager.GetSection
,它 returns 是一个对象,而不是列表或 ConfigurationSectionGroup
或 ConfigurationSection
。
我如何从我的自定义部分中获取值并循环遍历它们,假设只是在控制台中输出它们,它们的名称。
我的问题的完整答案可以在以下 SO 问题中找到:
Correct implementation of a custom config section with nested collections?
问题是我无法创建多个部分,但我可以通过集合创建该部分的多个元素。因此,我必须配置 类 以支持我的布局。请参阅上面发布的 SO 答案以获取完整示例。
我正在尝试在 Web 配置中创建一个部分,我可以在其中获得名称列表。
为此,我做了以下工作:
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 指南,它是这样完成的:https://msdn.microsoft.com/en-us/library/2tw134k3.aspx
我需要做的下一部分是获取(以 IEnumerable 格式、列表、数组,任何真正的格式)myCustomSection。我需要知道所有条目。但是,我似乎无法在任何地方找到它是如何完成的。查看 ConfigurationManager.GetSection
,它 returns 是一个对象,而不是列表或 ConfigurationSectionGroup
或 ConfigurationSection
。
我如何从我的自定义部分中获取值并循环遍历它们,假设只是在控制台中输出它们,它们的名称。
我的问题的完整答案可以在以下 SO 问题中找到:
Correct implementation of a custom config section with nested collections?
问题是我无法创建多个部分,但我可以通过集合创建该部分的多个元素。因此,我必须配置 类 以支持我的布局。请参阅上面发布的 SO 答案以获取完整示例。