0

I'm working on an ASP.NET Core 8 Web API project using Entity Framework Core. I've added my connection string to appsettings.json, but when I try to retrieve it using

Configuration.GetConnectionString("DefaultConnection")

it returns null.

Here's my setup - appsettings.json:

{
    "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=MyDb;Trusted_Connection=True;"
    }
}

Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Trying to get connection string
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
Console.WriteLine("Connection String: " + connectionString);

// Add services
builder.Services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(connectionString));

Console output:

Connection String: (null)

What I've tried:

  • Confirmed appsettings.json is set to "Copy if newer"
  • Checked casing of ConnectionStrings and DefaultConnection
  • Tried builder.Configuration["ConnectionStrings:DefaultConnection"] -> still null
  • Manually added builder.Host.ConfigureAppConfiguration(...) —> no change

Expected behavior:

I expect

builder.Configuration.GetConnectionString("DefaultConnection") 

to return the connection string defined in appsettings.json.

Environment:

  • ASP.NET Core 8.0
  • Windows 11
  • Visual Studio 2022 (latest)
  • .NET SDK 8.0.100

Question: why is GetConnectionString("DefaultConnection") returning null even though the key exists in appsettings.json, and how can I fix it?

6
  • 1
    Are you able to get any other configuration values in the appsettings file? Commented Jul 14 at 21:49
  • Do you have an appsettings.developpment.json in your project? If so, does it have a ConnectionStrings section? Commented Jul 14 at 21:53
  • 1
    Not sure if it matters but your console output and your code to write console output do not match. if connectionString is null I would expect Connection String: rather than Connection String: (null) ... which could mean you are not running the code you think you are running. Commented Jul 14 at 21:57
  • Some questions: did you check the files content on the bin folder? did you debug the code and look into the builder.Configuration to see its content? what is the folders and files structure, the appsettings should be on root folder? what is the Build Action property value for the appsettings.json file? Commented Jul 14 at 22:05
  • 1
    I wasn't able to replicate this with a new empty Web API project -- when I added the connection string in appsettings and builder.Configuration.GetConnectionString in Program.cs, the Console output showed the connection string instantly. Debugging your actual project to see builder.Configuration will definitely help. Commented Jul 14 at 22:24

1 Answer 1

0

Check if appsettings.json is being copied correctly

A common reason builder.Configuration.GetConnectionString("DefaultConnection") returns null is that the appsettings.json file isn’t getting copied to the build output folder (bin/), so the app can’t find it at runtime.

Here’s how to fix it:

  1. Right-click on appsettings.json in Solution Explorer

  2. Go to Properties

  3. Make sure these settings are applied:

  • Build ActionContent

  • Copy to Output DirectoryCopy if newer

enter image description here

Sometimes this gets missed, especially when copying files between projects or starting from a template. Without those settings, even if your code looks correct, the config won't load

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I appreciate the help... I’ll give it a try and see how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.