Application Settings (appsettings.json) in ASP.NET Core

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.

appsettings.json

The appsettings stores the configuration values in name-value pairs using the JSON format. A simple JSON format is as shown below

The name-value pairs may be grouped into multi-level hierarchy as shown below

The following is the sample appsettings.json file, which Visual Studio automatically creates when we create a new ASP.NET core application.

Here is another example with connection string & jwt related configuration

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

  1. appsettings.json
  2. 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 the appsettings.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.

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

  1. Import the namespace using Microsoft.Extensions.Configuration;
  2. Inject the IConfiguration 
  3. Use the getValue method to read the values

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

  1. Create a custom.json file in project root folder
  2. Select the file Right Click -> Properties -> Copy to output directory. Select Copy if newer or Copy always
  3. Open the program.cs. Use the AddJsonFile extension method to load the custom.json file.

The custom.json here is loads after the default configuration, hence any settings in will overwrite the values loaded in default configuration

Reference

2 thoughts on “Application Settings (appsettings.json) in ASP.NET Core”

  1. 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”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top