Different ways to get settings from appsettings.json file in .NET Core application

Updated on: May 26, 2021

There are some application level settings we store in our .NET application in web.config file, but with .NET Core, there is no web.config file, we are storing application settings in appsettings.json file in JSON format. We can use these appsettings in our .NET Core Web API or .NET Core MVC and .NET Core Blazor application. Below steps shows the different ways to read those settings from appsettings.json file and use it in your Controller file:

Step 1: Create the .NET Core MVC / Web API application as shown below:

dotnet new mvc -o AppsettingsNetCoreMVCApp

From Visual Studio => Create a New Project => Select "ASP.NET Core Web App (Model-View-Controller) template and click on Next button => Give Project Name "AppsettingsNetCoreMVCApp" and click on Next button => select the Target Framework and Authentication Type and click on Create button to create the .NET Core MVC application.

Step 2:  Now, open appsettings.json file and add the below settings to it:

  1. {
  2.   "MyAppSettings": {
  3.     "UserSettings": {
  4.       "UserName": "Test Username",
  5.       "EmailAddress": "testuser@test.com",
  6.       "SendEmails": "True"
  7.     }
  8.   },
  9.   "ConnectionStrings": {
  10.     "MyAppConnectionString": "Server=localhost\\mssqlserver;Database=testdb;Trusted_Connection=True;"
  11.   }
  12. }

You can add your custom appsettings as per the requirement. Here we have used 2 sections, one is MyAppSettings and another is "ConnectionStrings" section. Below code shows, how we can use these appsettings into our Controller class.

Step 3: Now, open HomeController.cs file and add the below code:

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;

  3. namespace AppsettingsNetCoreMVCApp.Controllers
  4. {
  5.     public class HomeController : Controller
  6.     {
  7.         private readonly IConfiguration _config;
  8.         public HomeController(IConfiguration configuration)
  9.         {
  10.             _config = configuration;
  11.         }

  12.         public IActionResult Index()
  13.         {
  14.             ViewBag.Username = _config["MyAppSettings:UserSettings:UserName"];
  15.             ViewBag.EmailAddress = _config.GetSection("MyAppSettings").GetSection("UserSettings").GetSection("EmailAddress").Value;
  16.             ViewBag.SendEmails = _config.GetValue<bool>("MyAppSettings:UserSettings:SendEmails");
  17.             ViewBag.ConnectionString = _config.GetConnectionString("MyAppConnectionString");
  18.             return View();
  19.         }
  20.     }
  21. }

As per the above code, we have added IConfiguration as the constructor parameter for HomeController class. We are using this IConfiguration to retrieve the key/value from appsettings.json file.

In the Index() Action Method, we have shown the different ways to read key/value from appsettings.json file:

1. First way we can directly use the IConfiguration reference and without any extension method we and get the values from array (_config[""]) as shown in line number 16, but remember to use ":"(Colon) and not "."(dot), it will not work if you add dot instead of colon.

2. We can use GetSection() method of IConfiguration to retrieve the settings from appsettings.json, refer Line Number: 17

3. We can use GetValue() method of IConfiguration to retrieve the key/value of appsettings.json, refer Line number: 18

4. We can retrieve the Database Connection string from appsettings.json file using GetConnectionString() method, refer Line Number: 19, but remember to declare "ConnectionStrings" without spelling mistake, otherwise it will not work.

Step 4: Now, add the below code in Index.cshtml page which will display appsettings values on the browser:

  1. @{
  2.     ViewData["Title"] = "Home Page";
  3. }

  4. <div class="text-center">
  5.     <h6>Username: @ViewBag.Username</h6>
  6.     <h6>Email Address: @ViewBag.EmailAddress</h6>
  7.     <h6>Send Emails: @ViewBag.SendEmails</h6>
  8.     <h6>Connection String: @ViewBag.ConnectionString</h6>
  9. </div>

Step 5: Now, run the application, it will show the appsettings values on the browser as mentioned on the Index.cshtml page:

You can try any of the above way to get the key/value from appsettings.json file.