How to use appsettings.json configuration in ASP.NET Core?

Configuration in ASP.NET Core based application is managed with one or more configuration files. Configuration providers read config data in the form of key value pair from multiple configuration sources and the sequence is –

In this article we will discuss about appsettings.json file in details. One more thing to note, Configuration providers that are added later overwrites previous key settings. For example, if a Key is available in both appsettings.json and the App Secrets, the App Secrets value is used, because it is loaded after appsettings.json and it overwrites the value.

How to manage appsettings.Environment.json file in .Net Core?

By default, an appsettings.json file and appsettings.Development.json file is created in .NET Core project.

We can create multiple appsettings file based on environment, for development we have appsettings.Development.json, for staging we can create appsettings.Staging.json and for production we can have appsettings.Production.json file.

How .NET Core loads config data based on environment?

In ASP.Net Core, we have multiple json file which helps developer to configure the project. Like appsettings.json, we have launchSettings.json file. The file contents looks similar to this –

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:23103",
      "sslPort": 44333
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Net5Demo": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

In launchSettings.json file, we have a key called ASPNETCORE_ENVIRONMENT. The value defined against this key tells about the environment of ASP.Net Core project.

If the value is Development, then key available in appsettings.Development.json file will be loaded and if the value is Production then key from appsettings.Production.json file will be loaded.

Let’s understand this by code.

To demonstrate this scenario, I have used Visual Studio 2019 and created a .NET 5 MVC project.

Open HomeController or any Controller created by you within this project and write below code.

private readonly IConfiguration configuration;

Add a constructor in Controller class.

  public HomeController(IConfiguration iconfiguration)
        {
            
            configuration = iconfiguration;
        }

In action method, write below code –

 public IActionResult Index()
        {
            string jsonValue = configuration.GetValue<string>("APIKey");
            return View();
        }

Your complete Controller class look like this –

    public class HomeController : Controller
    {
        private readonly IConfiguration configuration;
        public HomeController(IConfiguration iconfiguration)
        {
            configuration = iconfiguration;
        }
        public IActionResult Index()
        {
            string jsonValue = configuration.GetValue<string>("APIKey");
            return View();
        }
    }

In appsettings.json and appsettings.Development.json file add a key “APIKey” with different value in each json file, so that it can be identified.

{

  "APIKey": "Development API Key",

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

Now, open launchSettings.json and set the ASPNETCORE_ENVIRONMENT to Development.

Run your project and you may notice that in action method jsonValue variable will have value available in appsettings.Development.json.

Now, create another json file called, appsettings.Staging.json, add the same key in this file and in launchSettings.json set the ASPNETCORE_ENVIRONMENT to Staging.

You may notice that in action method jsonValue variable will have value available in appsettings.Staging.json.

What will happen if related environment file not found. In this case data will be loaded from appsettings.json if the given Key is available in this file else will return null value.

Watch this video to understand the use of appsettings.Environment.json file

Summary

Configuration can be managed with multiple options in ASP.Net Core project. appsettings.json and appsettings.Environment.json is one of the important config file available in ASP.Net Core project.

Config data loads from appsettings.Environment.json based on environment value defined in launchSettings.json file.

Hope this article is useful to you.

Please follow and like us:

Leave a Comment