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
- "DashboardSettings": {
- "Title": "C# Corner - A Community for Software Developers",
- "Header": {
- "IsSearchBoxEnabled": true,
- "BannerTitle": "Breaking News",
- "IsBannerSliderEnabled": false
- }
- }
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
- public class DashboardController : Controller
- {
- private readonly IConfiguration configuration;
- //requires using Microsoft.Extensions.Configuration
- public DashboardController(IConfiguration configuration)
- {
- this.configuration = configuration;
- }
- public IActionResult Index()
- {
- List<object> listSettings = new List<object>();
- var dashboardTitle = configuration["DashboardSettings:Title"];
- var isSearchBoxEnabled = configuration["DashboardSettings:Header:IsSearchBoxEnabled"];
- var headerBannerTitle = configuration["DashboardSettings:Header:BannerTitle"];
- var isBannerSliderEnabled = configuration["DashboardSettings:Header:IsBannerSliderEnabled"];
- listSettings.AddRange(new object[] { dashboardTitle, isSearchBoxEnabled, headerBannerTitle, isBannerSliderEnabled });
- //requires using Newtonsoft.Json
- return Content(JsonConvert.SerializeObject(listSettings));
- }
- }
Output

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.





Danny CrossleyPosted Aug 30, 2022, 5:14 PM
I got a lot out of this article. It was clear, logical and the code works. Thank you.
graeme thomsonPosted Apr 15, 2020, 11:06 PM
I would love to see a sample for managing a list. I need to see the JSON and the code to retrieve it. I have tried to do it myself but failed. The JsonConvert gets upset by the square brackets I use in my JSON file!
graeme thomsonPosted Apr 15, 2020, 11:04 PM
Very useful article.