5

In my project i have the following files:

appsettings.json and appsettings.Development.json

so the idea is to load the file appsettings.Development.json in the Development environement and the appsettings.json for the production environement.

in the launchSettings.json i changed the ASPNETCORE_ENVIRONMENT to Development:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }

this works as expected as the :

env.IsDevelopment() is true

the appsettings is loaded like this:

var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", true, true)
                .Build();

but still all the values are loaded from the appsettings.json and not the appsettings.Development.json.

i also tried:

var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", true, true)
                .AddJsonFile("appsettings.Development.json", true, true)
                .Build(); 

but the values are always loaded from the last file in this case appsettings.Development.json even if the :

env.IsDevelopment() is false
1

1 Answer 1

9

You need to compose the name of that environment specific settings file, instead of setting a fixed one.

$"appsettings.{env.EnvironmentName}.json"

For the Development environment it will be appsettings.Development.json.
For Production it will be appsettings.Production.json.

If you don't have such a file, only appsettings.json will be used, since you have set optional: true in that AddJsonFile call.

.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, true)

You might want to make appsettings.json a required file in order to not end up without any settings.

.AddJsonFile("appsettings.json", optional: false, true)

var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, true)
    .Build();
Sign up to request clarification or add additional context in comments.

1 Comment

yes exactly after few research i found that the person who created the project made a mistake by creating an istance of the ConfigurationBuilder everytime he wants to get a specific value from the config file. so what i did instead is injecting the IConfiguration in the controller and passing it to the service class, and the framework manages the EnvironmentName by extracting it from ASPNETCORE_ENVIRONMENT and i removed all the AddJsonFile methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.