我有一个应用程序,我正在尝试从 Azure 配置菜单读取环境变量。因此:
然后是一些读取环境变量的代码:
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.config
或appsettings.json
设置的。 - 如果我们想要覆盖这些值,我们需要将它们添加到 Azure 配置 => 应用设置中。
- 开发(本地)和 Azure 应用设置中的
AppSetting
的键名称必须相同。
在Azure AppSettings
中
我的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();
}
输出
关于.net - azure 应用程序服务从 web.debug.config 读取自定义环境变量而不是配置菜单变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73737057/