All About AppSettings In ASP.NET Core

Introduction

 
This article demonstrates how to add the keys and read the value from appsettings.json file in Asp.net Core. This article starts with an introduction of the appsettings file. After that, it demonstrates how to add new keys in appsettings file and then how to read the value from appsettings through IConfigurations and IOptions extensions.
 

Appsettings File

 
In the Asp.Net Core application we may not find the web.config file. Instead we have a file named "appsettings.json". So to configure the settings like database connections, Mail settings or some other custom configuration settings we will use the "appsettings.json". 
 
The appsettings in Asp.net core have different configuration sources as shown below.
  • appsettings.json File
  • Environment Variable
  • User Secrets
  • Command Line Arguments

Add New Keys

 
The appsettings.json file can be configured with Key and Value pair combinations. So, the Key will have single or multiple values.
  1. {  
  2.  "ConnectionStrings": {  
  3.     "MyDatabase""Server=localhost;Initial Catalog=MySampleDatabase;Trusted_Connection=Yes;MultipleActiveResultSets=true"  
  4.   }
  5. }  

Read Values from appsettings.json

 
To read the values from appsettings.json there are several ways to read. One of the simple and easy way to read the appsettings in Asp.net core is by using the IConfiguration using the namesapce Microsoft.Extensions.Configuration.
 
In the below code, we have two method to read the value using IConfiguration extension. Inject the IConfiguration in the controller constructor and use the variable to get the section.
 
 "config.GetSection("ConnectionStrings").GetSection("MyDatabase").Value" will return the value as "Server=localhost;Initial Catalog=MySampleDatabase;Trusted_Connection=Yes;MultipleActiveResultSets=true".
 
Similarly,
 
"config.GetValue<string>("ConnectionStrings:MyDatabase")" will return the value  as "Server=localhost;Initial Catalog=MySampleDatabase;Trusted_Connection=Yes;MultipleActiveResultSets=true".
  1. using Microsoft.Extensions.Configuration;              
  2. public class HomeController : Controller              
  3. {  
  4.     private readonly IConfiguration config;  
  5.     public HomeController (IConfiguration configuration)  
  6.     {  
  7.             config = configuration;  
  8.     }  
  9.     public IActionResult index()  
  10.     {  
  11.             // Method 1        
  12.             string _dbCon1 = config.GetSection("ConnectionStrings").GetSection("MyDatabase").Value;              
  13.             // Method 2
  14.             string _dbCon2 = config.GetValue<string>("ConnectionStrings:MyDatabase");              
  15.             return View();  
  16.     }  
  17. }  
The other method to read the values from appsettings is by using the custom class. Now we will see how to implement and read the values in Asp.net core by using IOptions using the namespace Microsoft.Extensions.Options.
 
Step 1
 
Open Visual Studio 2019 and Create a new project select here ASP.NET CORE web application template.
 
 
Step 2
 
Configure the new project by adding the Project name, Location and Solution name.
 
 
Step 3
 
Select the ".Net Core" and "ASP.NETCore 3.1" version and then select "Web application" as a project template.
 
 
Step 4
 
Select the appsettings.json file and add the configuration settings. Here i have added two configuration settings "ConnectionStrings" and "EmailSettings".
  • ConnectionString -> Will have the Database connection string settings.
  • EmailSettings -> Will have the email configure settings like Mail port, Server, Password. 
 
Step 5
 
Create a class with properties that matching appsettings.json.
 
The Properties name and the configuration settings name should be matched accordingly.
 
 
Step 6
 
Go to the Home controller, to read the values from appsettings.json file from custom class, inject the IOptions inside the controller constructor.
 
 
Step 7
 
To access configuration information in the Startup class, inject the IConfiguration service provided by the Framework. Startup class is in Startup.cs file.
 
 

Summary 

 
In this article, I discussed how we can add the configuration in app settings through Key-Value pairs and how to read the configuration from appsettings.json file in Asp.net Core using the extensions IConfiguration and IOptions.
 
Please add your valuable comments.
 
<Always be coding>