I want to get value using Appsettings from appsettings.json file
My code is in appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AppSettings": {
"APIURL": "https://localhost:44303/api"
},
"AllowedHosts": "*"
}
But I don't know how to get that value in common class file.
In general, you want to use strongly-typed configuration. Essentially, you just create a class like:
public class AppSettings
{
public Uri ApiUrl { get; set; }
}
And then, in ConfigureServices
:
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
Then, where you need to use this, you'd inject IOptions<AppSettings>
:
public class Foo
{
private readonly IOptions<AppSetings> _settings;
public Foo(IOptions<AppSettings> settings)
{
_settings = settings;
}
public void Bar()
{
var apiUrl = _settings.Value.ApiUrl;
// do something;
}
}