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"));
}
}
First off: The assembly name and namespace of Microsoft.Framework.ConfigurationModel has changed to Microsoft.Framework.Configuration. So you should use: e.g.
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7"
as a dependency in project.json
. Use beta5 or 6 if you don't have 7 installed.
Then you can do something like this in Startup.cs
.
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
}
If you then want to retrieve a variable from the config.json you can get it right away using:
public void Configure(IApplicationBuilder app)
{
// Add .Value to get the token string
var token = Configuration.GetSection("AppSettings:token");
app.Run(async (context) =>
{
await context.Response.WriteAsync("This is a token with key (" + token.Key + ") " + token.Value);
});
}
or you can create a class called AppSettings like this:
public class AppSettings
{
public string token { get; set; }
}
and configure the services like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
//mvc options
});
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
and then access it through e.g. a controller like this:
public class HomeController : Controller
{
private string _token;
public HomeController(IOptions<AppSettings> settings)
{
_token = settings.Options.token;
}
}
For .NET Core 2.0, things have changed a little bit. The startup constructor takes a Configuration object as a parameter, So using the ConfigurationBuilder
is not required. Here is mine:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<StorageOptions>(Configuration.GetSection("AzureStorageConfig"));
}
My POCO is the StorageOptions
object mentioned at the top:
namespace FictionalWebApp.Models
{
public class StorageOptions
{
public String StorageConnectionString { get; set; }
public String AccountName { get; set; }
public String AccountKey { get; set; }
public String DefaultEndpointsProtocol { get; set; }
public String EndpointSuffix { get; set; }
public StorageOptions() { }
}
}
And the configuration is actually a subsection of my appsettings.json
file, named AzureStorageConfig
:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;",
"StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=fictionalwebapp;AccountKey=Cng4Afwlk242-23=-_d2ksa69*2xM0jLUUxoAw==;EndpointSuffix=core.windows.net"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"AzureStorageConfig": {
"AccountName": "fictionalwebapp",
"AccountKey": "Cng4Afwlk242-23=-_d2ksa69*2xM0jLUUxoAw==",
"DefaultEndpointsProtocol": "https",
"EndpointSuffix": "core.windows.net",
"StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=fictionalwebapp;AccountKey=Cng4Afwlk242-23=-_d2ksa69*2xM0jLUUxoAw==;EndpointSuffix=core.windows.net"
}
}
The only thing I'll add is that, since the constructor has changed, I haven't tested whether something extra needs to be done for it to load appsettings.<environmentname>.json
as opposed to appsettings.json
.
.NET Core 3.0
Maybe it is not the best way to get a value from appsettings.json, but it is simple and it works in my application!!
File appsettings.json
{
"ConnectionStrings": {
"DefaultConnection":****;"
}
"AppSettings": {
"APP_Name": "MT_Service",
"APP_Version": "1.0.0"
}
}
Controller:
On top:
using Microsoft.Extensions.Configuration;
In your code:
var AppName = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("AppSettings")["APP_Name"];
With .NET Core 2.2, and in the simplest way possible...
public IActionResult Index([FromServices] IConfiguration config)
{
var myValue = config.GetValue<string>("MyKey");
}
appsettings.json
is automatically loaded and available through either constructor or action injection, and there's a GetSection
method on IConfiguration
as well. There isn't any need to alter Startup.cs
or Program.cs
if all you need is appsettings.json
.