Popular blog tags

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": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
  "AllowedHosts": "*",
  "API_URL": "thebestappsettingintheworld.ever"

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

   public class Startup
        public Startup(IConfiguration configuration, IWebHostEnvironment _webHostEnvironment)
            Configuration = configuration;
            //create configuration
            //Configuration = new ConfigurationBuilder()
            //    .SetBasePath(webHostEnvironment.ContentRootPath)
            //    .AddJsonFile("App_Data/appsettings.json", optional: false, reloadOnChange: true)
            //    .AddEnvironmentVariables()
            //    .Build();

            Configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)

        public IConfiguration Configuration { get; }


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. 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. 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. 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. Figure 4, accessing appsettings.json asp.net core, wrong value.