【发布时间】:2019-05-07 13:13:03
【问题描述】:
在检索 appsettings.json 中设置的 json 数组时遇到问题。
使用Configuration.GetSection("xxx").GetChildren()获取json数组时,返回值为null。然后我用下面的方法解决了这个问题,它奏效了。
在 appsettings.json 中:
{
"EndPointConfiguration": [
{
"UserName": "TestUser",
"Email": "Test@global.com"
},
{
"UserName": "TestUser",
"Email": "Test@global.com"
}
]
}
然后我创建类:
public class EndPointConfiguration
{
public string UserName { get; set; }
public string Email { get; set; }
}
最后,使用 EndPointConfiguration 类的数组即可:
var endPointConfiguration = Configuration.GetSection("EndPointConfiguration").Get<EndPointConfiguration[]>();
我对 .net 核心很陌生,不知道为什么 Configuration.GetSection().GetChildren() 不能工作。有高手能帮忙解答一下吗?谢谢。
【问题讨论】:
-
getChildren 返回
IConfigurationSection的列表,这是一个由键定义并具有值(docs) 的部分。对象数组不是IConfigurationSection,因为它不包含要引用的字符串键
标签: c# asp.net-core