How and where to define an environment variable on

2019-01-06 15:18发布

问题:

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.

回答1:

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:

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

You can verify the value from the debug console.

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



回答2:

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.



回答3:

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.