简体   繁体   中英

.NET Core how to specify environment to run application in

I have a Development, Staging, and Production server for my project. However when I run the project on my Development server it runs in Production mode. I can tell because it is using values from my appsettings.Production.json.

How do I specify that my project should run in development mode from this particular server? I've tried setting the environment variable ASPNETCORE_ENVIRONMENT = Development but that hasn't worked.

Please help. I have no idea how this is deciding to use the production environment. Any insight will help.

The environment settings are set in the operating system's environment variables, ie the bash profile. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1

But you can isolate the environment settings at launch time by setting up profiles. The dotnet run command with the --launchprofile parameter lets you specify which profile to use. This is what Visual Studio also uses when launching the app.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1

The variable set set in the launchSettings.json, you would need to build and publish with the appropriate 'launch' profile.

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54339/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express-local": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentsSample-staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM