One way to do it would be to create a Configuration Model based on your section/subsection from your appsettings.json file, add the configuration section in your Startup.cs class, then you can use the built-in dependency injection to inject the configuration anywhere you would need it.
For Example
appsettings.json :
{
"MyConfiguration" : {
"Configuration1" : "1",
}
}
Your configuration model:
public class MyConfiguration
{
public int Configuration1 { get; set; }
}
Then in the StartupClass in the ConfigureServices method you have to add
services.Configure<MyConfiguration>(configuration.GetSection("MyConfiguration"));
services.AddOptions();
And when you want to use that Configuration somewhere in the code, you would just inject it in the constructor like so:
public class SomeClass
{
private MyConfiguration MyConfiguration { get; }
public SomeClass(IOptions<MyConfiguration> myConfigurationOptions)
{
MyConfiguration = myConfigurationOptions.Value;
}
//... more code
}
Then you can access it
MyConfiguration.Configuration1