ASP.Net Core - How to Read values from Appsettings.json

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,

  1. Read all the values from the appsetting.json file using configuration.
  2. Get the Value from the appsetting.json file using Configuration.
  3. Read the value using GetSection
  4. Read the value using GetValue
  5. 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

  1. Basic knowledge of Asp.Net Core and appsetting,.json
  2. 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.

Asp.Net Core Web API project

Step 2. Provide Project Name and Location

Asp.Net Core Web API project

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

Asp.Net Core Web API project

Step 4. Set the appsetting.json file like below

Asp.Net Core Web API project

{
"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”.

[HttpGet("{key}")]

public string GetConfigValue(string key) {|
string str = "DatabaseConfig:" + key; return _configuration[str];
}

In the above example, we need to add a key and execute,

Step 8: Let’s read the value using GetSection. 

This is an example of how to use GetSection to read the config file.

GetSection Method - This method takes the section name as input and returns the values of the section. This method belongs to the “Microsoft.Extensions. Configuration” namespace – IConfiguration interface.

GetSection Method

Add the below code in the "ValuesController".

GetSection Method

[HttpGet("Section/{key}")]

public string GetSectionConfigValue (string key)
{
	string str = "DatabaseConfig: " + key; 
	return _configuration.GetSection(str).Value;
}

Execute and see the output

GetSection Method

GetSection Method

Step 9. Read value using GetValue method of configuration.

GetValue method

[HttpGet("GetValue/{key}")]|

public string GetValueConfigValue(string key)
{
    string str = "DatabaseConfig: " + key; 
    return _configuration.GetValue<string>(str);
}

Let's execute and see the output,

GetValue method

GetValue method

Read array from appsetting.json file.

Step 10. Add the below code in the appsetting.json file.

"DatabaseConfig": [
{
"DbName": "SQL SERVER",
"ConnectionString": "SQL Connection String",
"Port": "1234"
},
{
"DbName": "ORACLE",
"ConnectionString": "Oracle Connection String",
"Port": "5678"
}
]

Now we will add the below code in the “ValuesController”

Step 11. Create a new class called “Databaseconfig.cs”.

namespace ConfigDemo
{
    public class Databaseconfig
    {
       public string DbName { get; set; }
       public string ConnectionString { get; set; } 
       public string Port { get; set; }
    }
}

Add the below code in the “ValuesController”

[HttpGet]
public List<Database config> GetConfigList()
{
  return _configuration. GetSection("DatabaseConfig").Get<List<Databaseconfig>>();
}

Execute the code and see the below output,

Click on Execute button,

That’s all for this article. We have learned how to read the appsetting.json file value using Configuration, GetSection, and GetValue. Also, we have learned how to read array values from the appsetting.json file.

Hope you find this article helpful and we will discuss more ways to read appsetting. config values in upcoming articles.