0

I've configured the appsettings.json file and load it in the Main but am unable to access the values; it always returns null.
appsettings.json

{
  "smdbapi_url": "https://localhost:7084/Database?data="
}

Main

IConfiguration config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", true, true)
    .Build();

var services = new ServiceCollection();

ConfigureServices(services);
services
    .AddSingleton<Executor, Executor>()
    .BuildServiceProvider()
    .GetService<Executor>()
    .AppStart(config, args);

Executor

public void AppStart(IConfiguration config, string[] args)
{
    _config = config;

    _args = args;

    string? smdbapi_url = _config.GetValue<string>("smdbapi_url");
}
John Butler
  • 121
  • 6
  • 2
    Are you sure the JSON file is being copied to your build folder? What happens if you do this instead? `.AddJsonFile("appsettings.json", false, true)` – DavidG Jun 13 '23 at 14:38
  • Does this answer your question? [How to read AppSettings values from a .json file in ASP.NET Core](https://stackoverflow.com/questions/31453495/how-to-read-appsettings-values-from-a-json-file-in-asp-net-core) – Tu deschizi eu inchid Jun 13 '23 at 15:21

1 Answers1

0

DavidG's comment prompting me to check the build folder solved the issue. I'm accustomed to not having to check the build property of the appsettings.json file. So, pro tip :-), if you have to add the file you have to set it's build properties. Thanks, David...

John Butler
  • 121
  • 6