In this example we will describe what happens when you scaffold a new project.
First thing, the following dependencies will be added to you project (currently project.json
file) :
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
It will also create a constructor in your Startup.cs
file which will be in charge of building the configuration using ConfigurationBuilder
fluent api:
ConfigurationBuilder
.appsettings.json
to the configuration builder and monitor it's changesappsettings.environementName.json
configuration filepublic Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); }
If a same setting is set in several sources, the latest source added will win and its value will be selected.
Configuration can then be consumed using the indexer property. The colon :
character serve a path delimiter.
Configuration["AzureLogger:ConnectionString"]
This will look for a configuration value ConnectionString
in an AzureLogger
section.