104

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
severin
  • 5,203
  • 9
  • 35
  • 48
  • I've added an environment variable under system and user but when I pull them all I don't see the one I added. Does this only work in production environments? – Daniel Jackson Aug 24 '18 at 14:12

3 Answers3

172

Your problem is spaces around =.

This will work (attention to space before closing quote):

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

The space after ASPNETCORE_ENVIRONMENT in this code is not a typo! The problem in the question was having extra space (in SET...), so you must use the same space in GetEnvironmentVariable().

As noted by Isantipov in a comment, an even better solution is to remove the spaces entirely from the SET command:

set ASPNETCORE_ENVIRONMENT=Production
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Dmitry
  • 16,110
  • 4
  • 61
  • 73
  • 1
    Dmitry, where should we execute this "SET" command ... in CommandPrompt?? – Aswartha Sep 05 '16 at 08:12
  • 1
    Aswartha, It depends on your machine/scenario. You can run this in command prompt before "dotnet run" (in same command prompt) if starting from command prompt, or add to "My Computer" environment variables (for all apps on current machine), or [into `launchSettings`](https://docs.asp.net/en/latest/fundamentals/environments.html) (when staring from VS), or to WebApp configuration when running in Azure. – Dmitry Sep 05 '16 at 08:53
  • Okay, and what happens if we have `=` sign or space ` ` as part of environment variable value? How should we assign/fetch those? – nicks Dec 06 '16 at 05:12
  • 2
    If you have "strange" chars in value - no problem. Open command prompt, run `set myvar=bla=bla bla` and then `echo %myvar%` - you will see `bla=bla bla` – Dmitry Dec 06 '16 at 08:33
  • 3
    You wouldn't want to use 'ASPNETCORE_ENVIRONMENT ' (with a trailing space) - there are features in aspnet core which depend on the value of 'ASPNETCORE_ENVIRONMENT'(no trailing space) - e.g. resolving of appsettings.Development.json vs appsettings.Production.json. (I've added a bit of detail too long to be posted in a comment here: https://stackoverflow.com/a/47884810/1990682 ) – Isantipov Dec 20 '17 at 14:40
  • it works on netcore 3.1 – JRichardsz Oct 11 '21 at 21:44
28

This should really be a comment to this answer by @Dmitry (but it is too long, hence I post it as a separate answer):

You wouldn't want to use 'ASPNETCORE_ENVIRONMENT ' (with a trailing space) - there are features in ASP.NET Core which depend on the value of 'ASPNETCORE_ENVIRONMENT'(no trailing space) - e.g. resolving of appsettings.Development.json vs appsettings.Production.json. (e.g. see Working with multiple environments documentation article

And also I guess if you'd like to stay purely inside ASP.NET Core paradigm, you'd want to use IHostingEnvironment.Environment(see documentation) property instead of reading from Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") directly (although the former is of course set from the latter). E.g. in Startup.cs

public class Startup
{
    //<...>

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        Console.WriteLine("HostingEnvironmentName: '{0}'", env.EnvironmentName);
        //<...>
    }

    //<...>
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Isantipov
  • 19,491
  • 2
  • 26
  • 41
6

If you create the Environment variables at runtime during development then you will get null every time. You have to restart the visual studio because VS read EV at startup only.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Umer Waheed
  • 4,044
  • 7
  • 41
  • 62