c# - ASP.NET 核心 : AddEnvironmentVariables doesn't load variables

标签 c# asp.net-core environment-variables

我有一个 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 设置。在这里我遇到了奇怪的行为:

  1. 如果我使用 F5visual studio 运行应用程序 - 它看不到我的系统环境变量
  2. 如果我使用 dotnet MyApp.dllbin/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/

相关文章:

c# - Entity Framework 与相同实体类型但具有不同关系类型的多对多关系

c# - 如何在 Server.MapPath 的 ASP.Net Core 替代方法中获取绝对路径

c# - 像系统一样的工作流来改变代码流

batch-file - 将基于特定大小的日志旋转为日期/时间戳副本

python - Django Stripe API 关键问题

c# - 无法在类库应用程序中找到 security.dll 库

asp.net-core - 如何在目录名称中使用句点从 Asp .Net Core 提供静态文件

c# - "services.AddRazorPages();"实际上添加了哪些服务?

asp.net-core - 用每个 hangfire 作业的独特值(value)丰富 Serlilogs

ubuntu 似乎没有拿起 .bash_profile