C# – ConfigurationSection.Get() returns null

When you use ConfigurationSection.Get() to load an object from appsettings.json, it returns null if the section doesn’t exist. Since you’re probably not expecting this to be null, this can lead to problems surfacing in unexpected places, such as getting a NullReferenceException:

var section = Configuration.GetSection("WeatherClientConfig"); //returns an "empty" config section var weatherClientConfig = section.Get<WeatherClientConfig>(); //returns null if (weatherClientConfig.IsEnabled) // throws NullReferenceException { //do stuff }
Code language: C# (cs)

Note: If you’re using ASP.NET Core, you’ll be referring to the config via builder.Configuration or app.Configuration.

What’s really going on here is Configuration.GetSection() returns an “empty” ConfigurationSection if the section doesn’t exist in appsettings.json. Then when you call .Get() on this “empty” config section object, it returns null.

The root problem is the section is missing from appsettings.json, so I’ll go over troubleshooting steps for figuring out why it’s missing.

1 – Are you using the right section name?

The section name you use with Configuration.GetSection() needs to match the section name in appsettings.json. So verify that these match.

Let’s say you have the following appsettings.json:

{ "WeatherConfig": { "IsEnabled": true } }
Code language: JSON / JSON with Comments (json)

To make it load the “WeatherConfig” section, use this exact same name when using Configuration.GetSection():

var section = Configuration.GetSection("WeatherConfig");
Code language: C# (cs)

Note: I suggest having a model class with the same name as the section, and then using nameof(). This makes it easier to get things right.

2 – Are you using the right appsettings.json?

If the section name matches between appsettings.json and Configuration.GetSection(), then verify that you’re looking at the right appsettings.json.

You can check which appsetting.json file(s) the code is actually loading by looking at Configuration.Providers either with debugging breakpoints or with code, like this:

using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.FileProviders; //initialize configuration... Console.WriteLine("Here's where the JSON files are located"); foreach (var provider in Configuration.Providers) { if (provider is JsonConfigurationProvider jsonProvider && jsonProvider.Source.FileProvider is PhysicalFileProvider fileProvider) { Console.WriteLine(Path.Combine(fileProvider.Root, jsonProvider.Source.Path)); } }
Code language: C# (cs)

Here’s an example of what this outputs:

Here's where the JSON files are located D:\Projects\MoviesAPI\appsettings.json D:\Projects\MoviesAPI\appsettings.Development.json
Code language: plaintext (plaintext)

Take a look at these files. Are these files what you’re expecting? Figure out where they’re coming from. Typically, these are copied to the project’s build output folder.

In an ASP.NET Core project, you can loop through the providers (as shown above) by first casting builder.Configuration to IConfigurationRoot:

foreach (var provider in (builder.Configuration as IConfigurationRoot).Providers) { ... }
Code language: C# (cs)

Note: Or use app.Configuration. It depends on where you’re trying to use ConfigurationSection.Get().

3 – Are you copying appsetings.json to the output folder?

If you know the code is using the appsettings.json from the build output folder, and it doesn’t seem to be getting updated with changes you’re making in the project’s appsettings.json, then you need to set it to copy to the output directory.

You can do this in the appsettings.json file properties, or just put the following settings in your csproj file:

<Project Sdk="Microsoft.NET.Sdk"> <ItemGroup> <None Update="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> </ItemGroup> </Project>
Code language: HTML, XML (xml)

Leave a Comment