我有以下appSettings.json文件
"UrlManagement": {
"UrlList": [
{
"Microsoft":{
"resource":"xyz",
type": "iot"
},
"AWS":{
"resource":"abc",
"type": "storage"
},
}]
}
}
我上课
public class UrlManagement
{
public string CompanyName;
public URLList UrlList;
}
public class URLList
{
public string Resource;
public string Type;
}
我正在尝试获得以下这些值
private List<string> GetConfigurationDictionary()
{
var items = this._configuration.GetSection("UrlManagement:UrlList:0").
GetChildren().ToDictionary(x => x.Key, x => x.Value);
}
我正在获得x.Value的空值
您可以搜索UrlManagement:UrlList:0
部分,然后使用Get<List<UrlList>>()
展平所有type
和resource
,如以下代码:
private Dictionary<string, string> GetConfigurationDictionary()
{
Dictionary<string, string> keyValuePairs = this._configuration
.GetSection("UrlManagement:UrlList:0")
.Get<List<UrlList>>()
.ToDictionary(x => x.type, y => y.resource);
return keyValuePairs;
}
我希望这可以帮助您解决问题