CoderJony

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

In this article, we will understand, how can we read the AppSettings from appsettings.json file in ASP.NET Core 3.1.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

Step 1: Create a new ASP.NET Core project in Visual Studio 2019.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

Step 2: On this screen, you can provide your project name, solution name and location.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

Step 3: Here you can select any type of ASP.NET Core Web Application that you want to create. I have selected the API project. This will create an ASP.NET Core Web API application.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

Step 4: Open appsettings.json file. Create an AppSettings key and provide some key values inside it as I have highlighted in the below picture.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

You can also copy the above settings from here.

  "AppSettings": {
    "ApplicationName": "Coder Jony",
    "ApplicationUrl": "http://coderjony.com"
  }

Step 5: Create a new AppSettings.cs class file.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

Step 6: Add the properties here in this class with the same key names that you have defined in the AppSettings section of appsettings.json file.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

You can copy the code from here as well for AppSettings.cs file.

public class AppSettings
{
      public string ApplicationName { get; set; }
      public string ApplicationUrl { get; set; }
}

Step 7: Open StartUp.cs file, and add the highlighted lines in the ConfigureServices method like below.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

Copy the above-highlighted code from here.

// 1. Read app settings from AppSettings key from appsettings.json file
var appSettingsSection = Configuration.GetSection("AppSettings");

// 2. Register it with a strongly typed object to access it using dependency injection 
services.Configure<AppSettings>(appSettingsSection);

The above code does two things. First, it reads the AppSettings section from the file. Second, it creates a mapping of that with the AppSettings class and also registers it to be used as IOptions<AppSettings> using dependency injection.

Step 8: Finally, create a new controller and verify that we can access the AppSettings values in the code. For this purpose, I have created a new DemoController, as you can see in the below picture. And I am successfully able to read app settings values in code from appsettings.json file.

Read AppSettings in ASP.NET Core 3.1 from appsettings.json file

You can copy the above code from here as well.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace AppSettingsDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DemoController : ControllerBase
    {
        private readonly AppSettings _appSettings;

        public DemoController(IOptions<AppSettings> appSettings)
        {
            _appSettings = appSettings.Value;
        }

        [HttpGet]
        public bool Get()
        {
            string applicationName = _appSettings.ApplicationName;
            string applicationUrl = _appSettings.ApplicationUrl;

            return true;
        }
    }
}  

Thant's all :)

This is how we have successfully read the AppSettings section from the appsettings.json file in the ASP.NET Core application.

Buy Me A Coffee