You can add any environment and different app settings per environment easily. So you don't need to add any code the startup class in ASP.NET Core and everything works by default. You may want to have some code, probably when it comes to have a different feature (not a different config) for different environments (for example, exposing an endpoint just in staging environment), but for app settings, no code. Just go with the configs in proper files and choose the profile to run.
For example, if you want a different app settings for a new environment called Home, do the followings:
- Add a appsettings.Home.json file to the project.
- Add the setting to the file, for example:
{ "DetailedErrors": true, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "MyAppSettings": { "Title": "Hello from Home environment!" } }
- Add the value for the other environments as well.
- Now to choose the profile or environment, open launchSettings.json which is under Properties folder and set the environment in
ASPNETCORE_ENVIRONMENT
:{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:5061", "sslPort": 0 } }, "profiles": { "Home": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5110", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Home" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
This basically enables the Home environment:
Now when you run the application, it will read the app settings from the environment that you specified in launchsettings.json.
You can define or add any other environment that you want, using the lunchsettings.json or using the project properties as mentioned in the other answer.
To see it in action:
@page
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
@Configuration.GetSection("MyAppSettings")["Title"]
And if you run the app, with Home profile:
To learn more take a look at following resources: