Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration.GetSection returns null in vs code, but not in visual studio 19

I'm transitioning my .net core 3.1 app to run in VS Code instead of visual studio 19 and it builds and runs but I cannot read the appsettings.json file contents. It returns null.

In 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();

In my class:

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

here is what I see when I debug it in vs19, I clearly see the Data returning from my appsettings.QA.json file, but in vs code, it returns nothing. enter image description here

like image 434
Mike Avatar asked Sep 06 '25 03:09

Mike


1 Answers

I figured it out, it was an issue with the path in the launch.json

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

Needed to be: "cwd": "${workspaceFolder}/ProjectName",

In visual studio, when I created the project, it doubled up on the name of my project. Now it works fine.

like image 92
Mike Avatar answered Sep 09 '25 20:09

Mike