How and where to define an environment variable on Azure

I came across this question when facing the same problem myself. Well, just to give a clearer answer which I found at blog.elmah.io.

All you need to do is:

  1. Include the new appsetting.{env}.json in the publishOptions in file project.json.
  2. Go to Azure Portal and open the web application.
  3. Select application settings.
  4. Add an entry for ASPNETCORE_ENVIRONMENT and set it to whatever you want.

That's it.


In the new version 2021 go to the resource -> Configuration.

Then click on "New application settings".

Then add "ASPNETCORE_ENVIRONMENT" as key and click on OK then Save. enter image description here

Note that you must have the appsettings.(environment).json file for .Net Core.


I assume you are using Azure App Service (formerly known as Azure Websites).

To define your own environment variable, click to your site → All SettingsApplication settings:

Enter image description here

Add an app setting in the "App settings" section:

Enter image description here

You can verify the value from the debug console.

Going to https://{your site name}.scm.azurewebsites.net/DebugConsole:

Enter image description here


I know OP specified in the portal, but command line is more repeatable. To do it using Azure CLI:

az webapp config appsettings set -n $webappname -g $resourceGroupName --settings ConnectionStrings__Default=$connectionString

To set an environment variable ConnectionStrings__Default to the variable $connectionString