P-Finny
P-Finny

Reputation: 261

How to use appsettings.Development.json variables in code

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?

Upvotes: 8

Views: 8979

Answers (1)

P-Finny
P-Finny

Reputation: 261

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.....

Upvotes: 7

Related Questions