My env is VS Code and .NET Core 2.0.
I need to read a status code and several pairs of code/message from my appsetting.json.
This is my appsettings.json file
{
"http":
{
"statuscode": 200
},
"responses":
{
"data": [
{
"code": 1,
"message": "ok"
},
{
"code": 2,
"message": "erro"
}
]
}
}
I'm loading the configuration file and data like below, but everything is null:
private readonly IConfiguration _conf;
const string APPSET_SEC = "responses";
const string APPSET_KEY = "data";
public ValuesController(IConfiguration configuration)
{
_conf = configuration;
var section = _conf.GetSection($"{APPSET_SEC}:{APPSET_KEY}");
var responses = section.Get<string[]>();
var someArray = _conf.GetSection(APPSET_SEC).GetChildren().Select(x => x.Value).ToArray();
}
Either responses and someArray are null. It appears that the string array is not valid but it looks like a valid Json string array. What do I need to modify my appsettings or my C# code to get "data" array loaded into the variable?
I tried a simplified array in json file
{
"statuscode": 200,
"data": [
{
"code": 1,
"message": "ok"
},
{
"code": 2,
"message": "erro"
}
]
}
using the code:
var section = _conf.GetSection($"{APPSET_SEC}");
var responses = section.Get<string[]>();
but I still got no joy