dotnet run OR dotnet watch with development environment from command line?

65,955

Solution 1

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 ...

Solution 2

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.

Solution 3

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"
  },
}

Solution 4

Check documentation

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

https://docs.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/"
    },   
  }
}

Solution 5

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

Share:
65,955
Nexus23
Author by

Nexus23

A Developer.

Updated on July 10, 2022

Comments

  • Nexus23
    Nexus23 almost 2 years

    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
    
    • Ayyash
      Ayyash about 7 years
      Is it just me, or none of the answers below worked!
    • Ayyash
      Ayyash about 7 years
      Mm, I was using powershell, turned to command prompt and it worked... wonder why
  • Nexus23
    Nexus23 almost 8 years
    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.
  • Nexus23
    Nexus23 almost 8 years
    Thanks, I was setting the variable in separate window, works fine as you suggested. :)
  • Avi Kenjale
    Avi Kenjale almost 8 years
    @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
  • Christian.K
    Christian.K almost 8 years
    @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.
  • Avi Kenjale
    Avi Kenjale almost 8 years
    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.
  • Christian.K
    Christian.K almost 8 years
    @AviKenjale please ask a new questions using the "Ask Question" button at the top of the SO page. It is not useful to offload them into the comments, where they are less visible for other users and where there are insufficient capabilities to properly provide an answer.
  • Skorunka František
    Skorunka František almost 8 years
    Isn't it really possible to do it without setting the ENV variable? Passing an argument to DOTNET executable is more "systematic" IMHO.
  • Geir Sagberg
    Geir Sagberg over 7 years
    @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".
  • Technetium
    Technetium over 7 years
    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.
  • Technetium
    Technetium over 7 years
    @SkorunkaFrantišek it's possible with a few minor changes. I wrote up an answer to explain how.
  • Technetium
    Technetium over 7 years
    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.
  • Yury Scherbakov
    Yury Scherbakov over 7 years
    Exactly what is needed! Thank you
  • lex82
    lex82 about 7 years
    It seems the environment parameter doesn't exist anymore. It doesn't work for me and I can't find it in the docs: docs.microsoft.com/de-de/dotnet/articles/core/tools/dotnet-r‌​un
  • Technetium
    Technetium about 7 years
    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.
  • lex82
    lex82 about 7 years
    ok, sorry. I should have read your answer properly instead of just skimming it ;-)
  • MrJalapeno
    MrJalapeno almost 7 years
    Just had this question: stackoverflow.com/questions/45264806/… (should have read the comments of your answer). Now it runs, but any idea why dotnet run environment=development would still run in production mode?
  • MrJalapeno
    MrJalapeno almost 7 years
    Nevermind, forgot to add .UseConfiguration(configuration) to webhostbuilder.
  • Bishop
    Bishop about 6 years
    As an update to dotnetcore 2.0, the Microsoft.Extensions.Configuration.CommandLine package doesn't seem necessary now.
  • oleksa
    oleksa about 4 years
    Seems that dotnet run environment=staging does not work any more. However I can change the environment name using slightly different command line, like dotnet project1.dll --environment Production where Project1.dll is a ASP.NET Core project built for .NET Core 3.1
  • Technetium
    Technetium about 4 years
    @oleksa: Did you plug in the AddCommandLine(args) configuration parsing, or are you trying it without updating your Program.cs file?
  • oleksa
    oleksa about 4 years
    I'm using .AddJsonFile and .AddEnvironmentVariables() only. Should I use AddCommandLine to make environment=development key works?
  • Technetium
    Technetium about 4 years
    Yes, but for your host configuration as opposed to your application configuration. Those aren't the same thing. If you look above, I provided configuration to the WebHostBuilder which includes AddCommandLine(args), not configuration to the application.
  • Asad Ali
    Asad Ali about 4 years
    this works for me -> "dotnet run --launch-profile EnvironmentsSample"
  • Christian.K
    Christian.K over 3 years
    @Toolkit: I'm not the developer and I - of course - had no saying in this, merely saying how it is. Having that said, overwriting configuration file settings with environment variables is not retarded at all, but typical in many software products (albeit I'd agree a command line option in addition would be nice). Also this is not a "global" environment variable. As you can see, it is set in the shell (instance) from where you start the app. You could set it globally, but that is not the idea. Just as it is with many environment variable "solutions" you have the choice and can take a bad one.
  • Christian.K
    Christian.K over 3 years
    @Toolkit It should not be a production issue to set to "Development" mode. If you don't like the solution MS has taken, open an issue with them. Cheers.
  • Toolkit
    Toolkit over 3 years
    @Christian.K, so if it is not a "global" environment variable, but a shell one, I was wrong. It is an acceptable solution. Sorry mate
  • David Mays
    David Mays almost 2 years
    you can set the environment from the CLI using "dotnet watch run --environment Development".