asp.net-core.net-core.net-5.net-6.0microsoft.extensions.configuration

I can't use SeBasePath anymore when adding custom json file into IConfigrutaion?


I have a Class Library project based on .NET 6.

I'm trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that.

I installed Microsoft.Extensions.Configuration version 5.0.0

The below code is working for Microsoft.Extensions.Configuration version 3.0

But SetBasePath is not working anymore and I couldn't find the what is the new way?

How can I fix that?

public static IConfigurationRoot GetAppSettings()
{
    var applicationExeDirectory = ApplicationExeDirectory();

    var builder = new ConfigurationBuilder()
    .SetBasePath(applicationExeDirectory)
    .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true);
    return builder.Build();
}

Solution

  • I found the solution.

    I installed Microsoft.Extensions.Configuration.Json (v5) package and then SetBasePath and AddJsonFile methods worked!