Setting And Reading Values From "appsettings.json" In .NET Core


In this article, we are going to learn how to read values from  "appsettings.json" file. You will ask why we want to read values from "appsettings.json" and what is there in "appsettings.json" that we would want to read it.

The "appsettings.json" file contains configuration settings. This file is similar to the Web.config file. In this file, we mostly store global values so that we can read those in the entire application. But Web.config file is in XML format and "appsettings.json" file is JSON format. We store the connection strings, keys, and value pairs mostly in "appsettings.json" file.

The way of the reading the values of Connection string and keys and value pairs in .NET application is given below. 


But as we move towards .NET Core, the old style of reading values doesn’t work here. Let’s see the demo of how to set values and read values from "appsettings.json" file.

For doing this, let's create a .NET Core web application first.

Creating .Net Core web application

From Visual IDE, choose File Menu >> New >> Project.

After choosing the Project menu, a new dialog will pop up with the name “New Project”, as shown below.

Creating new Web application project and naming it “DemoCore”

In this dialog, just have a look at the left panel [Templates]. Inside that, you will see a new option [.NET Core]. Just choose [.NET Core].
After that, in the middle panel, you will find all project templates related to .NET Core. Select the  “ASP.NET Core Web Application (.NET Core)” template.
Now, we need to name the project. Here, we are naming this project as “DemoCore”. Finally, click on OK button to create the project.

In the next step, it asks for choosing project template. Here, we are going to choose "Web Application" template and click OK.

Choosing project template

Project Structure

The project structure after choosing the web application template:

Project structure

You can see that the"appsettings.json" file is by default available in the new web application project.

Dependencies required for Using IConfiguration interface in project.json file

Dependencies required for Using IConfiguration interface in project.json file

Next, open the "appsettings.json" file in Visual Studio IDE.

appsettings.json file

Appsettings.json file opened in edit mode

After opening the file in Visual Studio Editor, add the connection string, keys, and value pairs in it.

Adding Connection String and Key and Values in Appsettings.json file

 Adding new key and values in appsettings.json file

Adding new service in "ConfigureServices" Method in Startup class

In startup file, we need to add new Singleton instance in which we have passed Configuration class as instance. So, whenever we call IConfiguration Interface, their dependency injection will get an instance of Configuration class.

Note - In .NET core, dependency injection is a built-in feature.

Adding new service for reading values of Appsettings.json file

Code snippet of Adding service in Appsettings.json
  1. public void ConfigureServices(IServiceCollection services) {  
  2.     services.AddMvc();  
  3.     services.AddSingleton < IConfiguration > (Configuration);  
  4. }  
After adding the service, we will read the values of Connection string, keys and value pairs.

Reading values from "appsettings.json" file

We are going to use Constructor injection with the homeController for reading the values. For that, I have added a Constructor named HomeController (same as class name) which takes IConfiguration Interface as an input parameter.

At runtime, when we call HomeController, this Constructor [HomeController(IConfiguration iconfiguration)] will get called and it will get resolved by its concrete class [Configuration] which we have set in Startup class.

From now on, we are not going to create instance but we are going to ask for instance.

Reading values of Appsettings.json file

Code snippet of HomeController
  1. using Microsoft.AspNetCore.Mvc;  
  2. using Microsoft.Extensions.Configuration;  
  3. namespace DemoCore.Controllers {  
  4.     public class HomeController: Controller {  
  5.         IConfiguration _iconfiguration;  
  6.         public HomeController(IConfiguration iconfiguration) {  
  7.             _iconfiguration = iconfiguration;  
  8.         }  
  9.         public IActionResult Index() {  
  10.             // First way  
  11.             string value1 = _iconfiguration.GetSection("Data").GetSection("ConnectionString").Value;  
  12.             // Second way  
  13.             string value2 = _iconfiguration.GetValue < string > ("Data:ConnectionString");  
  14.             var EmailID = _iconfiguration["EmailID"];  
  15.             var Websitename = _iconfiguration["Websitename"];  
  16.             var WebsiteURL = _iconfiguration["WebsiteURL"];  
  17.             return View();  
  18.         }  
  19.         public IActionResult About() {  
  20.             ViewData["Message"] = "Your application description page.";  
  21.             return View();  
  22.         }  
  23.         public IActionResult Contact() {  
  24.             ViewData["Message"] = "Your contact page.";  
  25.             return View();  
  26.         }  
  27.         public IActionResult Error() {  
  28.             return View();  
  29.         }  
  30.     }  
  31. }  
Debugging View while reading values from Appsettings.json file

 Snapshot of home controller while reading values from Appsettings.json file

Up Next
    Ebook Download
    View all
    View all