appsettings.json
is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.development.json
, appsettngs.production.json
, etc. The ASP.NET Core configuration system loads the appsettings.json
and also the environment-specific appsettings
file based on the current environment.
Table of Contents
appsettings.json
The appsettings
stores the configuration values in name-value pairs using the JSON format. A simple JSON format is as shown below
1 2 3 4 5 6 | { "option1": "value1", "option2": 2 } |
The name-value pairs may be grouped into multi-level hierarchy as shown below
1 2 3 4 5 6 7 8 9 | { "subsection": { "suboption1": "subvalue1", "suboption2": "subvalue2" } } |
The following is the sample appsettings.json
file, which Visual Studio automatically creates when we create a new ASP.NET core application.
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" } |
Here is another example with connection string
& jwt
related configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*" , "ConnectionStrings": { "DefaultConnection":"Server=LocalHost;Database=TestDB;Integrated Security=false;uid=sa;pwd=TestDB@1234" }, "Jwt": { "Key": "aqdstfhdghethhjjkkkkkfffffdddd", "Issuer": "http://www.example.com", "Audience": "Any", "AccessTokenExpirationMinutes": 2, "RefreshTokenExpirationMinutes": 60, "AllowMultipleLoginsFromTheSameUser": false, "AllowSignoutAllUserActiveClients": true } } |
Environment Specific appsettings.json
The ASP.NET Core can load different appsettings.json
files based on the current environment.
The ASP.NET core reads the value of the ASPNETCORE_ENVIRONMENT variable, to determine the current environment. The CreateHostBuilder
method in the program.cs class reads the value of the ASPNETCORE_ENVIRONMENT
variable very early in the application. It then creates the IWebHostEnvironment
object, which we can use to read the current environment anywhere in the application. using the IHostingEnvironment.EnvironmentName
property.
The ASP.NET core uses the JSON configuration provider to read configurations. It reads it in the following order
appsettings.json
appsettings.<EnvironmentName>.json
 :
For example, if the current environment is Development, then the Âappsettings.development.json
is loaded. If the environment is Production, then it uses theappsettings.production.json
Â
Hence we can load different configurations based on the environment or app is running. Here is the code from the ConfigureWebHostDefaults
method which loads the Configuration.
1 2 3 4 5 6 7 8 9 10 11 12 | .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); ... ... } |
Note that the values loaded later in the configuration override any previous configuration. For Example, the values of appsettings.json
are overwritten by the appsettings.development.json
Reading the Value Using IConfiguration
To Read the configuration values in a controller or in any service class
- Import the namespace using
Microsoft.Extensions.Configuration
; - Inject the
IConfiguration
- Use the
getValue
method to read the values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using Microsoft.Extensions.Configuration; ... ... public class HomeController : Controller { private readonly IConfiguration _config; public HomeController(IConfiguration config) { _config = config; } public IActionResult Index() { var result = _config.GetValue<string>("Logging:LogLevel:Default"); //Information return View(); } } |
Precedence
The default configuration in ASP.NET Core starts, it loads the appsettings.json
first. And then it loads the appsettings.<EnvironmentName>.json
. App secrets ( only in Development
environment ) , Environment variables. & Command-line arguments.
Hence it is important to understand, any configuration loaded later will overwrite the values from the appsettings
If you want to some settings not to be overwritten then use the custom Json configuration file as shown in the next section.
Custom Json configuration file
Instead of appsettings.json, we can use different name for the file.
To Do that first
- Create a
custom.json
file in project root folder - Select the file Right Click -> Properties -> Copy to output directory. Select
Copy if newer
orCopy always
- Open the
program.cs
. Use theAddJsonFile
extension method to load thecustom.json
file.
1 2 3 4 5 6 7 8 9 10 11 12 | public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((ctx, bld) => { bld.AddJsonFile("custom.json"); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); |
The custom.json
here is loads after the default configuration, hence any settings in will overwrite the values loaded in default configuration
This statement isn’t technically correct:
“The ASP.NET core uses the JSON configuration provider to read configurations. It reads it in the following order
appsettings.json
appsettings..json :
For example, if the current environment is Development, then the appsettings.development.json is loaded. If the environment is Production, then it uses the appsettings.development.json”
The end should read:
“If the environment is Production, then it uses the appsettings.Production.json”
Thanks, Joseph Updated the article