fiskeboss fiskeboss - 3 months ago 185
ASP.NET (C#) Question

ASP .NET Core read environment variables

Running my ASP.NET Core-app 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 app does not seem to be able to read environment variables.

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


returns null

What am I missing?

Answer

Your problem is spaces around =.

This will work:

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

Or remove spaces:

set ASPNETCORE_ENVIRONMENT=Production