Dan
Dan

Reputation: 567

How do you get the correct appsettings.json to be used on server - deploying .NET Core app to IIS on Windows Server?

I have a ASP.NET Core web app.

And have the following appsettings files:

appsettings.DevelopmentServer.json
appsettings.Test.json
appsettings.Development.json
appsettings.Production.json

And on my dev server, I have the env variable EnvironmentName set to DevelopmentServer

But it's still using appsettings.json.

How do I get each server to use the correct appsettings.json file?

Upvotes: 0

Views: 2408

Answers (2)

Jeffrey Verpoort
Jeffrey Verpoort

Reputation: 151

The ASPNETCORE_ENVIRONMENT environment variable determines the AppSettings filename, as well as if user secrets are loaded.

However, sometimes, for some reason you want to override it or add extra files. For example in a (unit) test project. You can manually add (additional) JSON files to the configuration builder like this:

    var config = new ConfigurationBuilder()
    .AddJsonFile("AppSettings.DevelopmentServer.json")
    .AddUserSecrets(Assembly.GetExecutingAssembly(), true).Build();

    // Access
    config["variable"];

Just leaving it here as thought or input for someone else because I think your problem will be solved with the environment variable.

Here is more information about how to configure your own JSON file or an extra JSON file at startup for the whole application: Microsoft docs

Something really nice to consider is this parameter 'reloadOnChange' where you can update your appsettings file while running the application to take immediate effect.

Upvotes: 0

Vayth
Vayth

Reputation: 425

Use ASPNETCORE_ENVIRONMENT instead of EnvironmentName as environment variable.

You can refer the official docs for more information

Upvotes: 3

Related Questions