简体   繁体   中英

azure app service reading custom environment variables from web.debug.config not configuration menu variables

I have an application, for which I'm trying to read environment variables from the Azure configuration menu. As such:

在此处输入图像描述

Then some code to read environment variables:

        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";
        }

However, what I'm finding is, the values being read into the code when deployed to Azure are still from web.debug.config, which presumably it shouldn't even be looking at considering it should be in release mode in the app service.

How do I read custom environment variables from the app service configuration?

  • The Azure environment variables are set from web.config or appsettings.json by default.
  • If we want to override the values, we need to add them in Azure Configuration => App settings.
  • The key name of the AppSetting must be same in both Development(local) and Azure App Settings.

In Azure AppSettings

在此处输入图像描述

My 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();
        }

Output

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM