DEV Community

Discussion on: [ASP.NET Core] Get values from IConfiguration

Collapse
 
dr_dimaka profile image
Dmitry Pavlov • Edited

To get array values from appSettings.json you could use typed settings class like this:

public class SampleBook
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class AppSettings
{
    public string SampleText { get; set; }
    public int SampleNumber { get; set; }
    public bool SampleBoolean { get; set; }
    public List<string> SampleArray { get; set; }
    public SampleBook SampleBook { get; set; }
    public List<SampleBook> SampleBooks { get; set; }
    public SampleBook NullSample { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

... and initialize it using Microsoft.Extensions.DependencyInjection package via OptionsConfigurationServiceCollectionExtensions with this line of code

services.Configure<AppSettings>(configuration); 
Enter fullscreen mode Exit fullscreen mode

where configuration is IConfiguration and services - IServiceCollection

Hope that helps!
Digitally yours, the Coding-Machine.NET

Collapse
 
masanori_msl profile image
Masui Masanori

Thank you for your great comment!
I will try your suggestion on my next ASP NET Core project :)