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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With