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.

{
  "ConnectionStrings": {
    "MyDatabase": "Server=localhost;Initial Catalog=MySampleDatabase;Trusted_Connection=Yes;MultipleActiveResultSets=true"
  }
}

Read Values from appsettings.json

To read the values from appsettings.json there are several ways to read. One of the simple and easy ways to read the app settings in Asp.net core is by using the IConfiguration using the namespace Microsoft.Extensions.Configuration.

In the below code, we have two methods to read the value using the 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".
using Microsoft.Extensions.Configuration;

public class HomeController : Controller
{
    private readonly IConfiguration config;

    public HomeController(IConfiguration configuration)
    {
        config = configuration;
    }

    public IActionResult index()
    {
        // Method 1
        string _dbCon1 = config.GetSection("ConnectionStrings").GetSection("MyDatabase").Value;

        // Method 2
        string _dbCon2 = config.GetValue<string>("ConnectionStrings:MyDatabase");

        return View();
    }
}

The other method to read the values from app settings 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.

New project

Step 2. Configure the new project by adding the Project name, Location, and Solution name.

Project name

Step 3. Select the ".Net Core" and "ASP.NETCore 3.1" versions and then select "Web application" as a project template.

Web application

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, and Password. 

Database connection

Step 5. Create a class with properties that match appsettings.json.

The Properties name and the configuration settings name should be matched accordingly.

Configuration settings

Step 6. Go to the Home Controller, to read the values from the appsettings.json file from the custom class, and inject the IOptions inside the controller constructor.

Home controller

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.

 Startup class

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!