Introduction
Reading values from the configuration file and using it at runtime in the application is a common scenario in real-world applications. In the previous version of the .Net frameworks, we used to read configuration values from the Web. config file using ConfigurationManager.
In the latest versions of .Net Core, there are many ways available to read the values from the appsettings.json file and use them in runtime in the code.
We will try to cover all industrial popular ways to read the configuration in upcoming articles. In this article, we will start with IConfiguration.
We will cover,
- Read all the values from the appsetting.json file using configuration.
- Get the Value from the appsetting.json file using Configuration.
- Read the value using GetSection
- Read the value using GetValue
- Read array from appsetting.json file.
I will explain all the topics with practical examples. we will create an API project to learn all concepts.
Prerequisites
- Basic knowledge of Asp.Net Core and appsetting,.json
- Visual Studio 2022 with .Net Core 6.0 or higher version
Please follow the below steps to learn more about it,
Step 1. Create a New Asp.Net Core Web API project.

Step 2. Provide Project Name and Location

Step 3. Provide .Net Framework and click on Next button.

Step 4. Set the appsetting.json file like below

{
"DatabaseConfig": {
"Email": "[email protected]",
"DbName": "SQL SERVER",
"ConnectionString": "SQL Connection String",
"Port": "1234"
}
}
Step 5. Delete the existing controller "WealthController" and add a new controller called "ValuesController"

Step 6. Write the code below in the "ValuesController" to read the appsetting.json file section "DatabaseConfig".
Read all the values from the appsetting.json file using configuration.

using Microsoft.AspNetCore.Mvc;
namespace ConfigDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController: ControllerBase
{
private readonly IConfiguration configuration;
public ValuesController(IConfiguration configuration) { _configuration = configuration;
}
[HttpGet]
public IEnumerable<KeyValuePair<string, string>> GetConfig()
{
return _configuration.GetSection ("DatabaseConfig"). AsEnumerable();
}
}
Let’s execute and see the output,

Click on Execute button,

Step 7. Let's add another method to read the Key from the appsetting.json file. For Eg. We will read the value of Email from the “DatabaseConfig” section from the appsetting.json file.
Please add the code below in the “ValuesController”.
















Join the conversation! Your thoughts help the community grow.