105

I am using dotnet watch command to run asp.net core project. However, by default, it is picking up the Production as an environment.

I have tried both options using:

1) > dotnet watch ASPNETCORE_ENVIRONMENT=Development

2) > dotnet run ASPNETCORE_ENVIRONMENT=Development

But it still picks up production as an environment.

Note: In visual studio environment variable is set in project properties as Development by default and running from visual studio picks that variable.

Question is: How to run dotnet core project in development from command line using either?:

1) dotnet run
2) dotnet watch
2
  • Is it just me, or none of the answers below worked! Commented Apr 22, 2017 at 15:40
  • 1
    Mm, I was using powershell, turned to command prompt and it worked... wonder why Commented Apr 22, 2017 at 15:45

7 Answers 7

140

ASPNETCORE_ENVIRONMENT is an environment variable (and AFAIK) not a switch to the dotnet cli.

So what you would do is set it prior to using the tool:

rem Windows
C:\> set ASPNETCORE_ENVIRONMENT=Development
C:\> dotnet ...

rem Unix
$ export ASPNETCORE_ENVIRONMENT=Development
$ dotnet ...
Sign up to request clarification or add additional context in comments.

13 Comments

Have set an environment variable ASPNETCORE_ENVIRONMENT and its value to be Development, but still in command line it is picking up hosting environment as production. Wondering how .net core sets this variable when running with visual studio by setting this in project properties. Isn't there a commandline switch? In Asp.Net Rc1 we used to pass it in web command of project properties like --ASPNET_ENV Development.
Thanks, I was setting the variable in separate window, works fine as you suggested. :)
@Christian.k I think this will set entire environment to Development and cannot see any changes at application level. What if 1. Want to change only application specific 2. Any way to change from application, not expecting from project properties, thru some other way if I am building application using Visual Studio Code
@AviKenjale that Dennis entirely ob where you set the variable. If you set it n a shell / cmd.exe it will only be visible to whatever commands or programs you start from that shell, no influence on other apps started from other shells with (possibly) differently set env vars.
Great ! It is working as you said. However, still 1. How can change default port 5000 to something else using command line? 2. Is this set command same as launchSettings.json? Basically I am more interested to set environment variables, Launch URLs thru my Ubuntu's Visual Studio Code or terminal.
|
41

You don't have to use environment variables if you adjust how the WebHostBuilder processes its configuration. This is merely the default for dotnet new -t web. For example, if you wanted to be able to set the default environment to "development" instead of production and facilitate overriding the environment in the command line, you could do that by modifying the normal Program.cs code from this ...

    public static void Main(string[] args) {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseUrls("http://0.0.0.0:5000")
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }

... into something like this ...

    private static readonly Dictionary<string, string> defaults =
        new Dictionary<string, string> {
            { WebHostDefaults.EnvironmentKey, "development" }
        };

    public static void Main(string[] args) {
        var configuration =
            new ConfigurationBuilder()
                .AddInMemoryCollection(defaults)
                .AddEnvironmentVariables("ASPNETCORE_")
                .AddCommandLine(args)
                .Build();

        var host =
            new WebHostBuilder()
                .UseConfiguration(configuration)
                .UseKestrel()
                .UseUrls("http://0.0.0.0:5000")
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

        host.Run();
    }

Doing this, the environment variables would still work, but you can override it on the command line without any third-party dependencies, like so:

dotnet run environment=development
dotnet run environment=staging

This is actually what the yeoman generators do.

13 Comments

It's worth nothing that due to an implementation detail of Microsoft's environment variable configuration package, the ASPNETCORE_ prefix is stripped in the eyes of all other pieces of configuration. This is why you do not include the prefix on the command line.
It's also worth nothing that you'll need to add the Microsoft.Extensions.Configuration.CommandLine package as a dependency to your project if you had not yet done so. It will contain the AddCommandLine() extension method.
It seems the environment parameter doesn't exist anymore. It doesn't work for me and I can't find it in the docs: learn.microsoft.com/de-de/dotnet/articles/core/tools/dotnet-run
it never existed out-of-the-box, @lex82. My answer describes how to change Program.cs to make this possible - not that it's some kind of default functionality.
As an update to dotnetcore 2.0, the Microsoft.Extensions.Configuration.CommandLine package doesn't seem necessary now.
|
36

You can also set the variable inline when calling dotnet:

ASPNETCORE_ENVIRONMENT=Development dotnet run

I have found this is great for NPM scripts, but must always be called right before dotnet, e.g.:

{
  ...
  "scripts": {
    "start": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet run",
    "watch": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet watch"
  },
}

Note: This only works in OS X or Linux; for a cross-platform solution, you can use cross-env:

npm install cross-env -D

Then change the scripts to:

{
  ...
  "scripts": {
    "start": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet run",
    "watch": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet watch"
  },
}

1 Comment

@Konstantin You're right, it does not. I imagine adding npmjs.com/package/cross-env will do the trick though, e.g. "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet run".
31

Check documentation

https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run?tabs=netcore21

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1

dotnet run --launch-profile EnvironmentsSample

launchSettings.json

{ 
  "profiles": {    
    "EnvironmentsSample": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    },   
  }
}

1 Comment

this works for me -> "dotnet run --launch-profile EnvironmentsSample"
24

From Building Single Page Applications on ASP.NET Core with JavaScriptServices (styling added):

If you’re using PowerShell in Windows, execute $Env:ASPNETCORE_ENVIRONMENT = "Development"

If you’re using cmd.exe in Windows, execute setx ASPNETCORE_ENVIRONMENT "Development", and then restart your command prompt to make the change take effect

If you’re using Mac/Linux, execute export ASPNETCORE_ENVIRONMENT=Development

Comments

13

To set from CLI using dotnet run or dotnet watch:

dotnet run --environment Development

dotnet watch run --environment Development

5 Comments

I believe you're right, however when I type: dotnet run --environment Production - it still outputs "Hosting environment: Development", so not sure how to convince anyone this actually works
@PandaWood could you somehow have a typo somewhere? If I type: "dotnet run --environment noob" I receive "info: Microsoft.Hosting.Lifetime[0] Hosting environment: noob" but do get "development" if I misspell environment. You may also have accidentally manually overridden the hierarchy of where .NET looks for variables as these can be changed in Program.cs if you really wanted to.
The generated file Properties\launchSettings.json appears to override the environment variable(s) and command line parameter.
Works for .net core 6, just create a new appsettings.Custom.json, then run dotnet watch run --environment Custom
dotnet watch run --project ProjectName
1

With credit to @Technetium 's answer, here's an update to dotnetcore 2.0 that works really well for me:

public class Program
    {
        private static readonly Dictionary<string, string> defaults = new Dictionary<string, string> {{ WebHostDefaults.EnvironmentKey, "Development" }};
        private static IConfiguration config;
        public static void Main(string[] args)
        {
            config = new ConfigurationBuilder()
                .AddInMemoryCollection(defaults)
                .AddEnvironmentVariables("ASPNETCORE_")
                .AddCommandLine(args)
                .Build();
            BuildWebHost(args).Run();
        }
public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(config)
                /* Following setting already accounted for in CreateDefaultBuilder() : https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder?view=aspnetcore-2.0 */
                // .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();
    }

Thanks again, @Technetium

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.