How to read an appsettings.json value in ASP.NET Core

Published: Thursday 26 October 2023

In an ASP.NET Core Web API, you can inject theĀ IConfiguration instance as part of dependency injection.

Watch our video where we talk about how to read and get a value in ASP.NET Core:

We have this appsettings.json file:

	"Url": "",
	"Site": {
		"Title": "Round The Code",
		"Description": "ASP.NET Core tutorials"
	"Smtp": {
		"Port": 25

And we have this Web API controller that reads the configuration through IConfiguration and returns the Url key as part of the response for the SiteDetails endpoint.

public class SiteController : Controller {

	private readonly IConfiguration _configuration;

	public SiteController(IConfiguration configuration) {
		_configuration = configuration;
	public IActionResult SiteDetails() {
		return Ok(new {
			Url = _configuration.GetValue<string>("Url")

If we want to add Site:Title and Smtp:Port from appsettings.json to the SiteDetails endpoint response with a key of SiteTitle and SmtpPort respectively, how would we do it?