Reza Del
Reza Del

Reputation: 793

appsetting.development.json is note being picked up by code in .net core 3.1

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

Answers (1)

Julian
Julian

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

Related Questions