.net - azure 应用程序服务从 web.debug.config 读取自定义环境变量而不是配置菜单变量

标签 .net azure asp.net-mvc-4 azure-web-app-service

我有一个应用程序,我正在尝试从 Azure 配置菜单读取环境变量。因此:

enter image description here

然后是一些读取环境变量的代码:

        public static string GetPathString() 
        {
          string targetEnv = Environment.GetEnvironmentVariable("TARGET_ENVIRONMENT");
          if (targetEnv.Equals(Environments.Dev))
          {
            return "/RCHHRATool/";
          }
          else if (targetEnv.Equals(Environments.Azure))
          {
            return "/";
          }
          return "INVALID ENVIRONMENT SET IN ENVIRONMENT VARIABLES";
        }

但是,我发现,部署到 Azure 时读入代码的值仍然来自 web.debug.config,考虑到它应该处于 Release模式,它可能根本不应该考虑它应用服务。

如何从应用服务配置中读取自定义环境变量?

最佳答案

  • 默认情况下,Azure 环境变量是通过 web.configappsettings.json 设置的。
  • 如果我们想要覆盖这些值,我们需要将它们添加到 Azure 配置 => 应用设置中。
  • 开发(本地)和 Azure 应用设置中的 AppSetting 的键名称必须相同。

Azure AppSettings

enter image description here

我的Web.config

<appSettings>
      <add key="TARGET_ENVIRONMENT" value="TARGET_ENVIRONMENT Development" /> 
  </appSettings>

HomeController

 public static string GetPathString()
        {
            var appsettings = ConfigurationManager.AppSettings;
            var targetEnv = appsettings["TARGET_ENVIRONMENT"];            
            return targetEnv;
        }
  public ActionResult Index()
        {
            var EnvVariable = GetPathString();
            ViewBag.EnvVariable = EnvVariable;      
            return View();
        }

输出

enter image description here

关于.net - azure 应用程序服务从 web.debug.config 读取自定义环境变量而不是配置菜单变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73737057/

相关文章:

.net - Entity Framework 4 在两次执行相同的查询时返回到数据库

c# - 为什么 Mono 中的 Winforms 没有打开任何窗口?

c# - Windows 10 物联网生命周期(或 : how to property terminate a background application)

c# - 为什么 EntityFramework 的 LINQ 解析器以不同方式处理外部定义的谓词?

azure - 使用专用端点在应用服务上定期收到 403 IP Forbidden

Visual Studio 中的 Azure 服务总线托管标识返回 401 - token 颁发者无效

PROD 中的 asp.net secret

c# - 如何将连接字符串注入(inject) IDbContextFactory<T> 的实例?

c# - MVC 复选框分组

asp.net-mvc - 无法在 cshtml 中加载 ReportViewerForMvc 引用