我有一个 asp.net core
应用程序(.NET Core 2.1
)。 Startup
类的ConfigureServices
方法中有一段代码:
Configuration = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{_hostingEnvironment.EnvironmentName}.json", false, true)
.AddEnvironmentVariables("MyApp:")
.Build();
我还设置了 system 环境变量 MyApp:DumpFolder
以覆盖 appsettings.json
中的 DumpFolder
设置。在这里我遇到了奇怪的行为:
- 如果我使用 F5 从
visual studio
运行应用程序 - 它看不到我的系统环境变量 - 如果我使用
dotnet MyApp.dll
从bin/Debug
文件夹构建并运行它 - 它会正确加载变量。
我检查了 asp.net core
并看到它使用 Environment.GetEnvironmentVariables()
方法,默认情况下(没有指定的 EnvironmentVariableTarget
)检索变量从当前进程。我不明白为什么当我从 visual studio
运行应用程序时没有加载变量?
最佳答案
重新启动 Visual Studio。
您可能刚刚声明了环境变量,因此 Visual Studio 看不到它们。
关于c# - ASP.NET 核心 : AddEnvironmentVariables doesn't load variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870781/