I am working on a .NET Core project where I have an appsettings.json file for each environment (Dev, Prod). I want to load values for these appsettings files from the respective properties files (application.dev.properties, application.prod.properties).
I have tried using the Addpropertiesfile method like below to achieve this.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.JSON;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json");
How can I achieve what I am trying to do?
In asp.net core we have the
appsettings.json,appsettings.{Environment}.jsonlike the structure:Non-prefixed environment variables are environment variables other than those prefixed by ASPNETCORE_ or DOTNET_. For example, the ASP.NET Core web application templates set "ASPNETCORE_ENVIRONMENT": "Development" in
launchSettings.jsonFrom Configuration in ASP.NET Core,
To load values for different
appsettings.jsonfiles, we can try to get the environment variables firstly, then loads configuration from environment variable key-value pairs after readingappsettings.json,appsettings.{Environment}.jsonas the below code :result: