asp.net-mvc 如何以及在何处在 Azure 上定义环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34608769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:31:16  来源:igfitidea点击:

How and where to define an environment variable on Azure

asp.net-mvcazuremodel-view-controllerenvironment-variables

提问by jstadnicki

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

我有一个部署到 Azure 的 ASP.NET MVC Web 应用程序。我正在使用该GetEnvironmentVariable(...)方法读取我的设置。

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

问题是我找不到在 Azure 门户中定义此环境变量的方法。我在哪里可以这样做?

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

我不想将这些东西放在文件中。将存在的数据是凭据。

采纳答案by jstadnicki

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

实际上这解决了我的问题(我昨天发现的 - 它也在 Scott Hanselman 的博客上):

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

在 ASP.NET 和 Azure 中配置私有配置数据和连接字符串的最佳实践

TLDR;

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.

将设置放在一个单独的文件中,该文件不会转到存储库,也不会部署到服务器。接下来在应用设置中定义缺失的变量,如小敏吴所示。

回答by Xiaomin Wu

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

我假设您使用的是 Azure 应用服务(以前称为 Azure 网站)。

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:

转到 https://{您的站点名称}.scm.azurewebsites.net/DebugConsole:

Enter image description here

在此处输入图片说明

回答by binjiezhao

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.

我自己遇到同样的问题时遇到了这个问题。好吧,只是为了给出我在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.
  1. 在文件的 publishOptions 中包含新的 appsetting.{env}.json project.json
  2. 转到 Azure 门户并打开 Web 应用程序。
  3. 选择应用程序设置。
  4. 为 ASPNETCORE_ENVIRONMENT 添加一个条目并将其设置为您想要的任何内容。

That's it.

就是这样。

回答by Kishan Vaishnav

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

在新版本 2019 中,转到资源 -> 配置。

Then click on "New application settings".

然后单击“新应用程序设置”。

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

然后添加“ASPNETCORE_ENVIRONMENT”作为键,然后单击“确定”,然后单击“保存”。 在此处输入图片说明

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

请注意,您必须拥有 .Net Core 的 appsettings.(environment).json 文件。

回答by MarwaAhmad

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

(2019 版)导航到 Azure 云门户中的 AppService,然后导航到配置。