- Launch Settings
- AppSettings
- Extensions
Important - before doing anything, make sure that the current environment is not being set via an environment variable or anywhere in code. These will override any other attempt to set an environment.
Launch Settings
Create a new publish profile in Properties/launchSettings.json e.g the EnableMailing profile here, which copies the default profile but sets a different ASPNETCORE_ENVIRONMENT value:
"profiles": {
"IntranetCore.Web": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7242",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"EnableMailing": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7242",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Mailing"
}
}
The profile is launched by passing the name to the --launchSettings or -lp switch:
dotnet watch run --launchSettings EnableMailing
AppSettings
Create an appSettings.[EnvironmentName].json file otherwise the default appSettings will be used. In this case the file will be named appSettings.Mailing.json.
Extension method on IHostEnvironment
public static class EnvironmentExtensions
{
/// <summary>
/// Determines whether the current environment is "Mailing", which is set by specifying the
/// <c>EnableMailing</c> launch profile: <code>dotnet watch run --launch-profle EnableMailing</code>
/// Note - will be ignored if the environment is set using an environment variable.
/// </summary>
/// <param name="environment">IHostEnvironment</param>
/// <returns>bool</returns>
public static bool IsMailing(this IHostEnvironment environment) => environment.EnvironmentName == "Mailing";
}