snippetcsharpCritical
How to read AppSettings values from a .json file in ASP.NET Core
Viewed 0 times
netasphowfromcorejsonfilevaluesappsettingsread
Problem
I have set up my AppSettings data in file appsettings/Config .json like this:
I have searched online on how to read AppSettings values from .json file, but I could not get anything useful.
I tried:
I know with ASP.NET 4.0 you can do this:
But how do I do this in ASP.NET Core?
{
"AppSettings": {
"token": "1234"
}
}I have searched online on how to read AppSettings values from .json file, but I could not get anything useful.
I tried:
var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // nullI know with ASP.NET 4.0 you can do this:
System.Configuration.ConfigurationManager.AppSettings["token"];But how do I do this in ASP.NET Core?
Solution
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
And we have a POCO object representing the configuration:
Now we build the configuration in
Note that
If we want to inject our configuration to our controllers, we'll need to register it with the runtime. We do so via
And we inject it like this:
The full
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
services.AddOptions();
// Add our Config object so it can be injected
services.Configure(Configuration.GetSection("MyConfig"));
}And we inject it like this:
public class HomeController : Controller
{
private readonly IOptions config;
public HomeController(IOptions config)
{
this.config = config;
}
// GET: //
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
services.AddOptions();
// Add our Config object so it can be injected
services.Configure(Configuration.GetSection("MyConfig"));
}
}Code Snippets
{
"MyConfig": {
"ApplicationName": "MyApp",
"Version": "1.0.0"
}
}public class MyConfig
{
public string ApplicationName { get; set; }
public int Version { get; set; }
}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"));
}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);
}Context
Stack Overflow Q#31453495, score: 491
Revisions (0)
No revisions yet.