8

The Startup class contains

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    Console.WriteLine($"{env.EnvironmentName.ToString()}");

    if (env.IsDevelopment())
    {
        // For more details on using the user secret store see 
        // https://go.microsoft.com/fwlink/?LinkID=532709
        builder.AddUserSecrets();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

but env.EnvironmentName.ToString() returns "Production".

I already setup my ASPNETCORE_ENVIRONMENT to "Development" in launchSettings.json

4
  • 4
    How do you start the project? ASPNETCORE_ENVIRONMENT in project settings / launchSettings.json is only applied when starting from Visual Studio via F5 / Ctrl + F5. Starting it via dotnet or by any other means do not apply it. launchSattings.json is a pure VIsual Studio Mechanic
    – Tseng
    Oct 9, 2016 at 16:16
  • Is there anyway to set environment variables. I'm using visual studio code and dotnet core CLI for developement.
    – datkom
    Oct 10, 2016 at 7:37
  • If you run it from command like, use your environments shell commands to set the environment variable (CommandLine, Powershell, bash, or whatever MacOS uses too)
    – Tseng
    Oct 10, 2016 at 10:55
  • already did this 'set ASPNETCORE_ENVIRONMENT=Development' but still getting the wrong environment
    – datkom
    Oct 10, 2016 at 11:23

3 Answers 3

6

This usually happens when you have setup environment in web.config too.

For example, if you have environment setup as Production in launchSettings.json-

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },

And in web.config, if you have other environment Staging-

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
  </environmentVariables>
</aspNetCore>

In this case, you will get Staging when you are trying to read env.EnvironmentName in startup.cs

See if this helps.

5
  • Tried to put <environmentVariables> on web.config but still doesn't work. I'm using visual studio code and dotnet core CLI for developement.
    – datkom
    Oct 10, 2016 at 7:36
  • If you not using VS2015 then you can set variable from command prompt like this- set ASPNETCORE_ENVIRONMENT=Development and then run dotnet commands.
    – Sanket
    Oct 10, 2016 at 8:33
  • 1
    already set it set ASPNETCORE_ENVIRONMENT=Development. Still getting the same result
    – datkom
    Oct 10, 2016 at 11:22
  • I LOVE YOU! I spent FOREVER trying to figure this out... I am now bald Apr 27, 2020 at 14:13
  • Outdated for asp.net core 2+: there is no more web.config Jul 14, 2020 at 13:03
2

This might be helpful to some.

I was running locally against published files - that is not being run through visual studio F5. Setting the environment variable through the command line added it to the user environment variables which did not work.

What I had to do to get it to work was to add ASPNETCORE_ENVIRONMENT to the system environment variables. Then, after a reboot, it worked.

To get to environment variables: Right-click on "This PC", then properties. Then Advanced system settings. Then finally click on button "environment variables". In the next pop up add to system variables.

1

Got this working using powershell command:

$Env:ASPNETCORE_ENVIRONMENT = "Development"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.