Environment Variables & LaunchSettings.json in ASP.NET Core - ProgramIdea

Environment Variables & LaunchSettings.json

ASP.NET Core provides support for setting application behavior at runtime with environment variables.

ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at application startup from launchsetting.json file and store that value in IHostEnvironment.EnvironmentName.

You can create you own environment, but 3 are built in Development, Staging and Production.

Default value of ASPNETCORE_ENVIRONMENT is Production.

Whenever .NETCORE add a new project then it does include launchSettings.json file which is a configuration file that CLI tool actually figure out how this process get started. LaunchSettings.json contains the localhost url to launch information of application including port number.

The enironment can be configured in your project's debug profiles. Debug profiles specify the server to use when launching the application and any environment variable to be set.

Your project can have multiple debug profiles that set environment variable differentlly.

{

  "iisSettings": {

    "windowsAuthentication":false,

    "anonymousAuthentication":true,

    "iisExpress": {

      "applicationUrl":"http://localhost:54905",

      "sslPort": 44302

    }

  },

  "profiles": {

    "IIS Express": {

      "commandName":"IISExpress",

      "launchBrowser":true,

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT":"Development"

      }

    },

    "FirstApp": {

      "commandName":"Project",

      "launchBrowser":true,

      "applicationUrl":"https://localhost:5001;http://localhost:5000",

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT":"Development"

      }

    }

  }

}
demo