How do I transform appsettings.json in a .NET Core MVC project?

Determine EnvironmentName from Build Type

For anybody that would like to set the EnvironmentName based on the build type, there is the handy .UseEnvironment(environmentName) on WebHostBuilder (found in Program Main).

As long as the appropriate compilation symbols are set against the build configurations in your project, you can do something like this to determine the EnvironmentName:

public static void Main(string[] args)
{
    string environmentName;
#if DEBUG
    environmentName = "Development";
#elif STAGING
    environmentName = "Staging";
#elif RELEASE
    environmentName = "Production";
#endif

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseEnvironment(environmentName)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

    host.Run();
}

Leave a Comment