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

Time:09-21

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

enter image description here

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?

CodePudding user response:

  • 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

enter image description here

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

enter image description here

  • Related