How and where to define an environment variable on Azure

后端 未结 6 2244
臣服心动
臣服心动 2020-11-27 15:38

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

相关标签:
6条回答
  • 2020-11-27 16:01

    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.

    0 讨论(0)
  • 2020-11-27 16:04

    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:

    0 讨论(0)
  • 2020-11-27 16:04

    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.

    0 讨论(0)
  • 2020-11-27 16:09

    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

    0 讨论(0)
  • 2020-11-27 16:26

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

    Then click on "New application settings".

    Then add "ASPNETCORE_ENVIRONMENT" as key and click on OK then Save.

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

    0 讨论(0)
  • 2020-11-27 16:26

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

    0 讨论(0)
提交回复
热议问题