ASP.NET Core - Accessing Configuration Settings From Appsettings.json File

Introduction

 
Keeping configurations and reading them at runtime is an essential part of any application. In a .NET Framework application, we used to have configuration values in the web.config file and read them to use throughout our application. In an ASP.NET Core application, there are many places available where we can keep our configuration values and then use them at runtime anywhere in the application. Microsoft has provided us the default providers to read configurations from such places.
 

Appsettings.json

 
In an ASP.NET core application, the "appsettings.json" file is where we can keep configurations and read them using JsonConfigurationProvider provided by Microsoft.
 
In this article, we will see how to keep configuration values in appsettings.json file and how to read those values and use them in an application.
  • Read settings using IConfiguration
  • Access a strongly typed value using GetValue<T> method
  • Access a section using the GetSection method
  • Access section and bind with strongly typed class using the Bind method
Source Code
 
You can get the source code from GitHub.
 

Create an Asp.Net Core web application

 
Navigate to the appsettings.json file and paste the following settings related to our application's dashboard user interface:
 
appsettings.json
  1. "DashboardSettings": {  
  2.    "Title""C# Corner - A Community for Software Developers",  
  3.    "Header": {  
  4.       "IsSearchBoxEnabled"true,  
  5.       "BannerTitle""Breaking News",  
  6.       "IsBannerSliderEnabled"false  
  7.    }  
  8. }  
To read settings inside any service or controller from appsettings.json file, IConfiguration service instance is required which represents a set of key/value application configuration properties. We need to inject it through construction injection to get the instance from Dependency Injection System of ASP.NET Core.
 
Note
Hierarchical settings can be accessed using keys separated by a colon (:), as shown in the below example.
 
DashboardController.cs
  1. public class DashboardController : Controller  
  2. {  
  3.    private readonly IConfiguration configuration;  
  4.   
  5.    //requires using Microsoft.Extensions.Configuration  
  6.    public DashboardController(IConfiguration configuration)  
  7.    {  
  8.       this.configuration = configuration;  
  9.    }  
  10.    public IActionResult Index()  
  11.    {  
  12.       List<object> listSettings = new List<object>();  
  13.   
  14.       var dashboardTitle = configuration["DashboardSettings:Title"];  
  15.       var isSearchBoxEnabled = configuration["DashboardSettings:Header:IsSearchBoxEnabled"];  
  16.       var headerBannerTitle = configuration["DashboardSettings:Header:BannerTitle"];  
  17.       var isBannerSliderEnabled = configuration["DashboardSettings:Header:IsBannerSliderEnabled"];  
  18.   
  19.       listSettings.AddRange(new object[] { dashboardTitle, isSearchBoxEnabled, headerBannerTitle, isBannerSliderEnabled });  
  20.   
  21.       //requires using Newtonsoft.Json  
  22.       return Content(JsonConvert.SerializeObject(listSettings));  
  23.    }  
  24. }  
Output
 
Asp.net Core - Accessing Configuration Settings From Appsettings.json File
 

Accessing value strongly types using GetValue<T>

 
In the above code, we saw that the settings read as a string type only. Either they are a boolean, a number or a string. The GetValue<T> method gives access settings as strongly typed.
  1. var dashboardTitle = configuration.GetValue<string>("DashboardSettings:Title");  
  2. var isSearchBoxEnabled = configuration.GetValue<bool>("DashboardSettings:Header:IsSearchBoxEnabled");  
  3. var headerBannerTitle = configuration.GetValue<string>("DashboardSettings:Header:BannerTitle");  
  4. var isBannerSliderEnabled = configuration.GetValue<bool>("DashboardSettings:Header:IsBannerSliderEnabled");  
Output
 
Asp.net Core - Accessing Configuration Settings From Appsettings.json File
 

Accessing Configuration Sections using the GetSection(key)

 
Want to read a particular section and then later on its values? Use the following code,
  1. var headerSection = configuration.GetSection("DashboardSettings:Header");  
  2. var isSearchBoxEnabled = headerSection.GetValue<bool>("IsSearchBoxEnabled");  
  3. var headerBannerTitle = headerSection.GetValue<string>("BannerTitle");  
  4. var isBannerSliderEnabled = headerSection.GetValue<bool>("IsBannerSliderEnabled");  
Output
 
Asp.net Core - Accessing Configuration Settings From Appsettings.json File
 

Accessing section and bind with strongly typed class using the Bind method 

If you want to read settings and bind with a strongly typed class, then you can use the "Bind" method of IConfiguration. Using this Bind method, you can bind the given class object instance to configuration values by matching property names against configuration keys recursively.
 
This is important to note that the hierarchy of properties in class must be matched with a hierarchy of keys of the configuration section inside the appsettings.json file.
 
Create a class inside the Models folder which should have properties names similar to keys of the section you want to read.
 
DashboardSettings.cs
  1. public class DashboardSettings  
  2. {  
  3.     public string Title { getset; }  
  4.     public DashboardHeaderSettings Header { getset; }  
  5. }  
  6. public class DashboardHeaderSettings  
  7. {  
  8.     public bool IsSearchBoxEnabled { getset; }  
  9.     public string BannerTitle { getset; }  
  10.     public bool IsBannerSliderEnabled { getset; }  
  11. }  
 
DashboardController.cs
  1. public class DashboardController : Controller  
  2. {  
  3.     private readonly IConfiguration configuration;  
  4.   
  5.     //requires using Microsoft.Extensions.Configuration  
  6.     public DashboardController(IConfiguration configuration)  
  7.     {  
  8.         this.configuration = configuration;  
  9.     }  
  10.     public IActionResult Index2()  
  11.     {  
  12.         var dashboardSettings = new DashboardSettings();  
  13.         configuration.Bind("DashboardSettings", dashboardSettings);  
  14.   
  15.         //requires using Newtonsoft.Json  
  16.         return Content(JsonConvert.SerializeObject(dashboardSettings));  
  17.     }  
  18. }  
In above code, we used Bind method of IConfiguration, which expects two parameters.
 
The first parameter expects "SectionName" (DashboardSettings in my case) which you want to bind. the second parameter expects an object instance to which you want to bind.
 
Output
 
 

Summary

 
In this article, we learned how to access configuration settings from the appsettings.json file in an ASP.NET a web application. In the next article, we will see an Options Pattern to read configuration settings.
 
Thanks a lot for reading. I hope you loved this article. Please share your valuable suggestions and feedbacks and write in the comment box if you have any questions.
 
Have a good day!