Introduction
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 - public void ConfigureServices(IServiceCollection services) {
- services.AddMvc();
- services.AddSingleton < IConfiguration > (Configuration);
- }
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 - using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- namespace DemoCore.Controllers {
- public class HomeController: Controller {
- IConfiguration _iconfiguration;
- public HomeController(IConfiguration iconfiguration) {
- _iconfiguration = iconfiguration;
- }
- public IActionResult Index() {
-
- string value1 = _iconfiguration.GetSection("Data").GetSection("ConnectionString").Value;
-
- string value2 = _iconfiguration.GetValue < string > ("Data:ConnectionString");
- var EmailID = _iconfiguration["EmailID"];
- var Websitename = _iconfiguration["Websitename"];
- var WebsiteURL = _iconfiguration["WebsiteURL"];
- return View();
- }
- public IActionResult About() {
- ViewData["Message"] = "Your application description page.";
- return View();
- }
- public IActionResult Contact() {
- ViewData["Message"] = "Your contact page.";
- return View();
- }
- public IActionResult Error() {
- return View();
- }
- }
- }
Debugging View while reading values from Appsettings.json file Snapshot of home controller while reading values from Appsettings.json file