Ben Donnelly
Ben Donnelly

Reputation: 1261

Net Core Environments Not Switching

I am having trouble making my environments switch between development and staging.

It stays at staging no matter what the environment variable is.

Here is my launchsettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "http://localhost/WebService",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:51658/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },
    "WebService": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:51659/"
    }
  }
}

No matter what launch profile I use, the environment remains at 'Staging'

As far as appsettings.X.json go, I have 3.

appsettings.json
appsettings.Development.json
appsettings.Staging.json

Upvotes: 0

Views: 220

Answers (2)

daremachine
daremachine

Reputation: 2798

I think it depend what you use for maintaining application Docker, IIS or something.

Docs says:

Per IIS Application Pool

To set environment variables for individual apps running in isolated Application Pools (supported on IIS 10.0+), see the AppCmd.exe command section of the Environment Variables topic.

You probably need run applications on isolated environment and for that env you need set that variable.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments

Upvotes: 0

Jerodev
Jerodev

Reputation: 33216

You have to set the environment by setting the windows environment variables. The correct appsettings.json file will be loaded using this environment.

This can be done using the command line by executing

set ASPNETCORE_ENVIRONMENT=Development

This website shows how to set the environment for different operating systems.

Upvotes: 1

Related Questions