gpt4 book ai didi

.net - 如何在 .NET Core 的 IConfiguration 中获取自定义环境变量

转载 作者:行者123 更新时间:2023-12-05 07:25:22 26 4
gpt4 key购买 nike

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(a => {
a.AddEnvironmentVariables();
})
.UseStartup<Startup>();

在我这样调用的 API 中,它不支持自定义变量,如何获取自定义变量我已经在系统中创建了一个自定义变量。

public ActionResult<IEnumerable<string>> Get() {
var t = configuration.GetSection("yash2").Value;
return new string[] { "value1", "value2" };
}

最佳答案

我之前给出了错误的答案。谢谢 James 指出这一点。

要将环境变量加载到应用程序,您需要使用__分隔符存储变量。

yesh2__config1="config1Value"
yesh2__config2="config2Value"

然后它将在此处可用。

configuration.GetSection("yash2")

除了加载所有环境变量,您还可以传递要加载的 key 。

 a.AddEnvironmentVariables("yesh2");

关于.net - 如何在 .NET Core 的 IConfiguration 中获取自定义环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54859674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com