Specifying Environment from the Command Line in ASP.NET Core

ASP.NET Core picks up the environment from the environment variable ASPNETCORE_ENVIRONMENT. If you want to set it from Visual Studio, you can do so by double clicking “Properties” in Solution Explorer > Debug > Environment Variables. Just add an entry with a name of “ASPNETCORE_ENVIRONMENT” and a value desired.

If you want to set the environment from the command line, you have to modify the Main method like this.

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder().AddCommandLine(args).Build();

    var host = new WebHostBuilder()
        .UseConfiguration(config)
        .UseKestrel()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

With that, you can pass the environment, like so.

dotnet run --environment "UAT"

You can also specify server URL using command line.

dotnet run --environment UAT --server.urls http://*:5001

That will set the environment to “UAT” and let Kestrel use port 5001.

3 thoughts on “Specifying Environment from the Command Line in ASP.NET Core

  1. There are no immediate plans to make the Kindle version available. I’m working on it though and it will take some time.

  2. Hi Badi,

    I see that you have self-published “The 201 on Building Web API with ASP.NET Core MVC” and that is fine. But can you please make a kindle version available? I don’t do “paperback” anymore and it is inconvenient.

    – Dennis Landi

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.