How to read AppSettings values from a .json file in ASP.NET Core
This has had a few twists and turns. I've modified this answer to be up to date with ASP.NET Core 2.0 (as of 26/02/2018).
This is mostly taken from the official documentation:
To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration
in your application’s Startup
class. Then, use the Options pattern to access individual settings. Let's say we have an appsettings.json
file that looks like this:
{
"MyConfig": {
"ApplicationName": "MyApp",
"Version": "1.0.0"
}
}
And we have a POCO object representing the configuration:
public class MyConfig
{
public string ApplicationName { get; set; }
public int Version { get; set; }
}
Now we build the configuration in Startup.cs
:
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
}
Note that appsettings.json
will be registered by default in .NET Core 2.0. We can also register an appsettings.{Environment}.json
config file per environment if needed.
If we want to inject our configuration to our controllers, we'll need to register it with the runtime. We do so via Startup.ConfigureServices
:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
}
And we inject it like this:
public class HomeController : Controller
{
private readonly IOptions<MyConfig> config;
public HomeController(IOptions<MyConfig> config)
{
this.config = config;
}
// GET: /<controller>/
public IActionResult Index() => View(config.Value);
}
The full Startup
class:
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
}
}
.NET Core 3.0 / .NET 6
This is a sleek and simple solution.
File appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "****;"
},
"AppSettings": {
"APP_Name": "MY_Service",
"SampleIntValue": 100
}
}
Controller:
On top:
using Microsoft.Extensions.Configuration;
In your code:
VAR 1:
var AppName = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("AppSettings")["APP_Name"];
or
VAR 2: (For reading multiple values or numbers)
var MyConfig = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var IntExample = MyConfig.GetValue<int>("AppSettings:SampleIntValue");
var AppName = MyConfig.GetValue<string>("AppSettings:APP_Name");