Really struggling with this one - is it supposed to be this hard?!
I have a simple object array in my appsettings:
"AppSettings": {
"Names": [
{
"Id": "1",
"Name": "Mike"
},
{
"Id": "2",
"Name": "John"
}
]
}
I then have a class
public class AppSettings
{
public List<Names> Names { get; set; } = new List<Names>();
}
public class Names
{
public string Id { get; set; }
public string Name { get; set; }
}
I read in my app settings:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json").Build();
var config = new AppSettings();
This is where stuff goes wrong:
config.Names = configuration.GetSection("AppSettings:Names") //<<<< what do I do here?
It seems like it's all tied to IConfigurationSection
which is not helpful.
var config = new AppSettings(); Configuration.GetSection("AppSettings").Bind(config);
It should work alike for just the names section if you prefer that – Suellen