Reputation: 793
In my current application i am using the following setup to read all the values from appsetting.json
public class AppSettings
{
private static AppSettings _appSettings;
public string RememberMeCookieName{ get; set; }
public AppSettings(IConfiguration config)
{
this.RememberMeCookieName = config.GetValue<string>("RememberMeCookieName");
_appSettings = this;
}
public static AppSettings Current
{
get
{
if (_appSettings == null)
{
_appSettings = GetCurrentSettings();
}
return _appSettings;
}
}
public static AppSettings GetCurrentSettings()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
var settings = new AppSettings(configuration.GetSection("AppSettings"));
return settings;
}
now my issue is that even with
.AddEnvironmentVariables();
the value from appsettings.json is being pick up instead of appsettings.Development.json
Any help would be welcomed
Upvotes: 4
Views: 3773
Reputation: 36
Try adding a second .AddJsonFile call after the first, using string interpolation for the environment name:
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
This is assuming you have environment variable ASPNETCORE_ENVIRONMENT set to "Development".
Upvotes: 1