What Works and What Doesn't With ConfigurationManager.AppSettings Using ASP.NET Core 3.1

Written by Ken Dale

ConfigurationManager has long been used by .NET Framework developers prior to .NET Core to access things like app settings and connection strings. By including the System.Configuration.ConfigurationManager NuGet package one can get existing code to compile on .NET Core 3.1 (netcoreapp3.1), but it may not work as you’d expect.

Simply put, the only location I was able to read an app setting locally was from app.config. appsettings.json and web.config did not seem to work.

Startup.cs

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
    {
        var str = ConfigurationManager.AppSettings["myApp:setting1"]; // null
        var str2 = ConfigurationManager.AppSettings["abc"];           // null
        var str3 = ConfigurationManager.AppSettings["test"];          // null
        var str4 = ConfigurationManager.AppSettings["webconfigtest"]; // null
        var str5 = ConfigurationManager.AppSettings["appconfigtest"]; // "abc"

        await context.Response.WriteAsync("");
    });
});

web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="webconfigtest" value="abc" />
  </appSettings>
</configuration>

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="appconfigtest" value="abc" />
  </appSettings>
</configuration>

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "abc": "123",
  "myApp": {
    "setting1": "abc"
  },
  "appSettings": {
    "test": "123"
  }
}

Published January 03, 2020 by

undefined avatar
Ken Dale Github Senior Application Developer (Former)

Suggested Reading