Implementing a new environment in ASP.NET Core

  • 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";
}
Last updated: 6/5/2026 11:23:59 AM

Latest Updates

© 0 - 2026 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com