Reading Values From Appsettings.json file in .Net Core
In this article, we are going to learn how to fetch properties or values from appsettings.json in .NET Core. There are one or more ways available to fetch values from appsettings.json file in .NET Core. Following are the popular two ways we are going to discuss:
Using IConfiguration
The IConfiguration
is available in the dependency injection (DI) container, so you can directly access JSON properties by
simply injecting IConfiguration
in the constructor of a controller or class.
It represents a set of key/value
application configuration properties.
Below is the appsettings.json file:
{ "Logging": { "LogLevel": { "Default": "Information", "Warning": "Warning", "Error": "Error" } }, "AllowedHosts": "*" }
In any simple .Net core API project, define one readonly property of type IConfiguration
and inject in your controller
and access JSON properties as follows:
namespace DotNetPalaceDemo.Controllers { using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System.Collections.Generic; [Route("api/Configuration")] [ApiController] public class ConfigurationController : ControllerBase { private readonly IConfiguration _config; public ConfigurationController(IConfiguration config) { _config = config; } // GET: api/Configuration [HttpGet] public IEnumerableGet() { var result = _config.GetValue ("Logging:LogLevel:Default"); // "Information" return new string[] { result.ToString() }; } } }
Using Options Pattern
The first option we discussed is easy to use, but if you want to access multiple values then you need to provide the keys and it shows hard coded values in controllers or classes everywhere. To access multiple values or hierarchy/structured data from appsettings.json, Options Pattern is a good option. It uses classes to represent the group of hierarchy/structured settings.
Let's say we need to read values from above appsettings.josn file. So, we'll create a DTO object (a simple class to represent the hierarchy/structured settings of appsettings.json
file like below:
//AppSettingsDTO.cs namespace DTOObjects { public class AppSettings { public Logging Logging { get; set; } public string AllowedHosts { get; set; } } public class Logging { public LogLevel LogLevel { get; set; } } public class LogLevel { public string Default { get; set; } public string Warning { get; set; } public string Error { get; set; } } }
Register in the configuration instance in startup.cs class of ConfigureServices method as follows:
public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection("Logging")); services.AddControllers(); }
Define one readonly property of type IOptions and inject in your controller and access JSON properties as follows:
namespace DotNetPalaceDemo.Controllers { using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using System.Collections.Generic; [Route("api/Option")] [ApiController] public class OptionController : ControllerBase { private readonly IOptions_logging; public OptionController(IOptions<Logging> logging) { _logging = logging; } // GET: api/Option [HttpGet] public IEnumerable<string> Get() { var result = _logging.Value.LogLevel.Default; // "Information" return new string[] { result }; } } }
Like above example your can create your DTOs based on your appsettings.json file structure and get the corresponding values of DTO's properties.
Summary
To summarise, there are two approaches to read the values from the appsettings.json file.
Both approaches are easy to implement and use but I would recommend you choose IOptions
method because in the grand scheme of things your
code will be cleaner and performance will be better. Hope you picked up a thing or two.
Cheers!