Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use appsettings.Development.json variables in code

Tags:

asp.net-core

I have an appsettings.json and appsettings.Development.json. I need to assign the SmtpServer name depending on the environment.

The config file in appsettings.json is:

{ 
  "EmailConfiguration": {
   "SmtpServer": "mail.MYDOMAIN.com"
  }
}

And in appsettings.Development.json is:

{ 
  "EmailConfiguration": {
    "SmtpServer": "mail.MYLOCAL.com"
  }
}

When I assign the configuration in Startup ConfigureServices() like such:

var emailconfig = Configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>();
services.AddSingleton<IEmailConfiguration>(emailconfig);

It always uses appsettings.json ('mail.MYDOMAIN.com') and NOT appsettings.Development.json.

How do I modify this code to use the correct environment settings?

like image 310
P-Finny Avatar asked Jan 02 '18 19:01

P-Finny


1 Answers

The answer is that all I needed was the environment variables in the 2 different appsettings files. My problem was a missing closing bracket in one of my configuration settings which meant that the appsettings.Development.json would not replace appsettings.json variables since they didn't match. Oops.....

like image 167
P-Finny Avatar answered Oct 04 '22 06:10

P-Finny