I am new to .Net Core and trying to get a value from the appsettings.json
file but I have missed something very basic. Please let me know what I have done wrong...
Here is the code...
Program.cs
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
})
Startup.cs
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
}
Web API Controller
private readonly IConfiguration config;
public EmailController(IConfiguration configuration)
{
if (configuration != null)
{
config = configuration;
}
}
Action Method
var emailTemplatesRelativePath = config.GetSection("EmailSettings");
var email = config.GetValue<string>("Email");
Both the above lines are returning null values for both GetSection
and GetValue
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"FCRContext": "server=xxx;database=xxx;user id=xxx;password=xxx"
},
"AllowedHosts": "*",
"EmailSettings": {
"EmailTemplatesPath": "EmailTemplates"
},
"Email": "aa@aa.com"
}