In previous article, I mentioned Send Email in ASP.NET Core (With Attachments) but in this article, we will look at how we can read or get the values from the appsettings.json file in ASP.NET Core C# and use them in our application step by step.
To demonstrate this, we will be creating a new ASP.NET Core MVC Application and read the values from the appsettings.json file and display these values on the view.
Step 1 – Create a new ASP.NET MVC Core Application in Visual Studio
We will create a new ASP.NET Core Web APP (MVC) application using .NET Core 3.1 (or .NET 5) from the visual studio Create a new project template and name it "ReadAppSettingsMvcApp".
Step 2 – Update the appsettings.json file to add new configuration
Consider you are creating an application in which you want to make company settings like Name, Email, and Contact Number configurable. So, in visual studio, open the appsettings.json file, and let’s add a block called CompanySettings.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"CompanySettings": {
"Name": "XYZ Enterprises",
"Email": "xyz@gmail.com",
"Contact": "1234567890"
},
"ProjectName":"Test"
}
Step 3 – Create a model class to map Company Settings
In this step, we will create a new class that we can use to map the settings from appsettings.json. So, Add a new class called “CompanySettings” and add properties for Name, Email and Contact as shown below.
namespace ReadAppSettingsMvcApp
{
public class CompanySettings
{
public string Name { get; set; }
public string Email { get; set; }
public string Contact { get; set; }
}
}
Step 4 – Update HomeController to read Company Settings
In .NET Core, to read appSettings.json we need the interface IConfiguration from Microsoft.Extensions.Configuration namespace. So we will inject this interface into the HomeController constructor and read the settings in the constructor. Update HomeController as shown below.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ReadAppSettingsMvcApp.Models;
using System.Diagnostics;
namespace ReadAppSettingsMvcApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
this.configuration = configuration;
}
public IActionResult Index()
{
return View();
}
}
}
Now let's add a new Action called “CompanyDetails”, in which we will read the company details from the app settings and pass them to the view. We will create the view for this Action in the next step. Now your Home Controller should look like -
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ReadAppSettingsMvcApp.Models;
using System.Diagnostics;
namespace ReadAppSettingsMvcApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
this.configuration = configuration;
}
public IActionResult CompanyDetails()
{
CompanySettings companySettings = new CompanySettings();
configuration.GetSection("CompanySettings").Bind(companySettings); // bind with model
var CompanyName = configuration.GetValue<string>("CompanySettings:Name"); //get's only name -> XYZ Enterprises
var ProjectName = configuration.GetValue<string>("ProjectName"); //get's output = Test
return View(companySettings);
}
public IActionResult Index()
{
return View();
}
}
}
Let’s quickly walk through the code of CompanyDetails action.
On the first line, we create the instance of CompanyDetails, then we asked the IConfiguration to get the section named "CompanySettings" from the app settings file and bind it to the instance of company details.
And then we passed it to the view.
The other 2 lines of code
var CompanyName = configuration.GetValue<string>("CompanySettings:Name"); //get's only name -> XYZ Enterprises
var ProjectName = configuration.GetValue<string>("ProjectName");// get's output = Test
Allow's you to get individual key-value from appsettings.json in .NET Core
Step 4 – Create a view to display Company Settings -
Now let’s add a new view in the Views -> Home folder. And the following code in the CompanyDetails.cshtml
file.
@model CompanySettings
@{
ViewData["Title"] = "Company Details";
}
<h2>Welcome to the Company!!</h2>
<hr />
<h4>Name : @Model.Name</h4><br />
<h4>Email : @Model.Email</h4><br />
<h4>Contact Number : @Model.Contact</h4><br />
Final Result -
Now that we have all the code in place, let’s run our application. And Navigate to https://localhost:44336/Home/CompanyDetails and that’s it, you can see the company details displayed on your screen which are read from appsettings.json.
I hope this article helped you. Thanks for your time.
You may also like to read:
Difference between Visual Studio Build, Re-build and Clean, solution