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.

Advertisements

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s