bcwiniger
bcwiniger

Reputation: 169

.NET Core Web API not reading environment correctly

I have a .NET Core 2.0 Web API that is failing to read the environment specific app.settings file. As far as I can tell I have followed all the relevant steps.

This is my Startup.cs constructor:

public Startup(IHostingEnvironment env)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();
}

I then have an appsettings.json file structure of:

appsettings.json
  -appsettings.debug.json
  -appsettings.release.json
  -appsettings.production.json

And on the server where my API is deployed to I have a system environment variable key/value pair of

Variable = ASPNETCORE_ENVIRONMENT Value = Release

But when I make calls into the API the appsettings.production.json values are used, which (from what I understand) is the default when an environment variable of ASPNETCORE_ENVIRONMENT is not found.

Thoughts on what my setup might be missing to cause this default behavior?

Upvotes: 5

Views: 3398

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239450

You haven't specified, but I'm assuming your running in IIS. If that's the case, you need to edit the advanced properties of your App Pool and set the "Load User Profile" option to true. Otherwise, it won't actually read from your environment variables.

Upvotes: 16

Related Questions