86

I have an ASP.NET MVC web application deployed to Azure. I'm reading my setting using the GetEnvironmentVariable(...) method.

The problem is that I can't find a way to define this environment variable in Azure Portal. Where can I do that?

I don't want to have this stuff in a file. Data that will be there are credentials.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaroslaw Stadnicki
  • 1,178
  • 1
  • 8
  • 17
  • Possible duplicate of [How do you put environmental variables in web.config?](http://stackoverflow.com/questions/29416302/how-do-you-put-environmental-variables-in-web-config) – Abhijit Annaldas Jan 05 '16 at 10:23
  • There's quite a lot of different ways to host an ASP.NET MVC application in Azure. You should specify that you're using Azure App Service, if that's the case. Otherwise there's a lot of different potential answers. – Jim Aho Dec 23 '20 at 08:26

7 Answers7

137

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xiaomin Wu
  • 3,621
  • 1
  • 15
  • 14
44

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.

Kishan Vaishnav
  • 2,273
  • 1
  • 17
  • 45
35

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
binjiezhao
  • 567
  • 8
  • 12
10

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

Lee Richardson
  • 8,331
  • 6
  • 42
  • 65
2

(2019 version) Navigate to your AppService in the Azure cloud portal then Configuration.

MarwaAhmad
  • 808
  • 9
  • 21
0

Actually this solves my problem (which I found yesterday - and it's on Scott Hanselman's blog also):

Best practices for private config data and connection strings in configuration in ASP.NET and Azure

TLDR;

Put settings in a separate file that doesn't go to the repository and are not being deployed to the server. Next define missing variables in app settings like Xiaomin Wu showed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaroslaw Stadnicki
  • 1,178
  • 1
  • 8
  • 17
  • 11
    For posterity, could you include the relevant parts of the link which you found useful in your answer? That way, if the link goes down, changes, etc., your answer won't be rendered useless. Many thanks. – Wai Ha Lee Jan 06 '16 at 21:51
0

In these modern times, when publishing from Visual Studio 2019, on the publish page there is a link called "Manage Azure App Service settings". In there you can create and define environment variables that get set when you publish. Very easy if you know where to look!

John
  • 357
  • 2
  • 7