How to read, unable to read appsettings.json values ASP.NET Core

I wrote this article here “Database connection string when swapping between App Servers slots” and if you look at the last paragraph I mentioned a ‘gotcha’ when you have an application setting, “AppSetting” in your web.config file which has the same name as an application setting you have configured, via the portal of your Azure App Settings.

Let’s start with the code.  I want to read from my appsettings.json file from within one of my ASP.NET Core controllers, for example.

First, add a setting to the applicationsettings.json file, similar to the following with a name of API_URL, for example and give it a value.

{
   "Logging": {
    "LogLevel": {
    "Default": "Warning"
    }
   },
    "AllowedHosts": "*",
    "API_URL": "thebestappsettingintheworld.ever"
}

Next, add this line of code to the ConfigureServices() method within the Startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IConfiguration>(Configuration);
}

You can read more about the AddSingleton() method here.

Finally, in the controller of choice, add the following code.

using Microsoft.Extensions.Configuration;

namespace mostawesomenamespaceever.Controllers
{
    public class HomeController : Controller
    {
       readonly IConfiguration _configuration;
   
       public HomeController(IConfiguration configuration)
       {
          _configuration = configuration;
       }

      public IActionResult About()
       {
         var URL = _configuration["API_URL"];
         ViewData["Message"] = $"Your application description page. The URL value is {URL}";
         return View();
       }
    }
}

I decided to get the API_URL setting within the Home Controller and when the client clicked on the About link.  I included the Microsoft.Extension.Configuration class, created a readonly configuration interface, a HomeController constructor that accepted the configuration as a parameter, set the referenced instance of the configuration to the local instance and finally in the About() method I used the local instance and queried it for API_URL.  When I deployed it to my Azure App Service I got what I expected, Figure 1.

image

Figure 1, accessing appsettings.json asp.net core, wrong value

What you need to look out for is if you have an app setting configured for your Azure App Service, with the same name, see Figure 2.

image

Figure 2, accessing appsettings.json asp.net core, wrong value

Then what you will see when the About() method is executed is that shown in Figure 3.

image

Figure 3, accessing appsettings.json asp.net core, wrong value

Instead of what you have placed into the appsettings.json file.  I wanted to find out why and started looking over the code.  I found a copy of the Microsoft.Extension.Configuration .dll on my workstation and decompiled it using Just Decompile and looked over the code for about an hour, see Figure 4.  I did not find the code path which would execute that causes such behavior.

image

Figure 4, accessing appsettings.json asp.net core, wrong value

I ran out of time looking for the code path.  If you happen to find it, post a comment.  I would love to review it and better understand it.