Read environment variables in ASP.NET Core

severin picture severin · May 23, 2016 · Viewed 93.6k times · Source

Running my ASP.NET Core application using DNX, I was able to set environment variables from the command line and then run it like this:

set ASPNET_ENV = Production
dnx web

Using the same approach in 1.0:

set ASPNETCORE_ENVIRONMENT = Production
dotnet run

does not work - the application does not seem to be able to read environment variables.

Console.WriteLine(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

returns null

What am I missing?

Answer

Dmitry picture Dmitry · May 23, 2016

Your problem is spaces around =.

This will work:

Console.WriteLine(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

Or remove spaces (better, see comment of @Isantipov below):

set ASPNETCORE_ENVIRONMENT=Production