I met a problem when retrieving the json array set in appsettings.json.
When using Configuration.GetSection("xxx").GetChildren() to get json array, and the return value is null. Then I used the way below to solve the problem, and it worked.
In appsettings.json:
{
"EndPointConfiguration": [
{
"UserName": "TestUser",
"Email": "[email protected]"
},
{
"UserName": "TestUser",
"Email": "[email protected]"
}
]
}
Then I create the class:
public class EndPointConfiguration
{
public string UserName { get; set; }
public string Email { get; set; }
}
Finally, using an array of the EndPointConfiguration class will work:
var endPointConfiguration = Configuration.GetSection("EndPointConfiguration").Get<EndPointConfiguration[]>();
I am pretty new to .net core, do not why the Configuration.GetSection().GetChildren() cannot work. Can anyone proficient help to give an answer? Thanks.
IConfigurationSectionwhich is a section defined by a key and having a value(docs). An array of objects is not aIConfigurationSectionas it does not contain a string key to be referenced