【问题标题】:Configuration.GetSection returns null in vs code, but not in visual studio 19Configuration.GetSection 在 vs 代码中返回 null,但在 Visual Studio 19 中不返回
【发布时间】:2020-11-19 01:21:54
【问题描述】:

我正在转换我的 .net core 3.1 应用程序以在 VS Code 而不是 Visual Studio 19 中运行,它可以构建并运行,但我无法读取 appsettings.json 文件内容。它返回 null。

在 Startup.cs 中:

       var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();

在我的课堂上:

var dbConnection = Configuration.GetSection("Database")["ConnectionString"];

这是我在 vs19 中调试时看到的,我清楚地看到从我的 appsettings.QA.json 文件返回的数据,但在 vs 代码中,它什么也没返回。

【问题讨论】:

    标签: visual-studio-code configuration asp.net-core-3.1


    【解决方案1】:

    我想通了,这是launch.json中路径的问题

    是: "cwd": "${workspaceFolder}",

    需要: "cwd": "${workspaceFolder}/ProjectName",

    在 Visual Studio 中,当我创建项目时,它在我的项目名称上翻了一番。现在可以正常使用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多