Search code examples
.netjsonasp.net-coreapp-configappsettings

How to check if Configuration Section exists in .NET Core?


How can you check if a configuration section exists in the appsettings.json in .NET Core?

Even if a section doesn't exist, the following code will always return an instantiated instance.

e.g.

var section = this.Configuration.GetSection<TestSection>("testsection");

Solution

  • Query the children of Configuration and check if there is any with the name "testsection"

    var sectionExists = Configuration.GetChildren().Any(item => item.Key == "testsection"));
    

    This should return true if "testsection" exists, otherwise false.