dotnet run OR dotnet watch with development environment from command line?
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 effectIf you’re using Mac/Linux, execute
export ASPNETCORE_ENVIRONMENT=Development
Comments
-
Nexus23 almost 2 years
I am using
dotnet watch
command to run asp.net core project. However, by default, it is picking up theProduction
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 about 7 yearsIs it just me, or none of the answers below worked!
-
Ayyash about 7 yearsMm, I was using powershell, turned to command prompt and it worked... wonder why
-
-
Nexus23 almost 8 yearsHave 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 almost 8 yearsThanks, I was setting the variable in separate window, works fine as you suggested. :)
-
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 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 almost 8 yearsGreat ! 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 aslaunchSettings.json
? Basically I am more interested to set environment variables, Launch URLs thru my Ubuntu's Visual Studio Code or terminal. -
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 almost 8 yearsIsn't it really possible to do it without setting the ENV variable? Passing an argument to DOTNET executable is more "systematic" IMHO.
-
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 over 7 yearsIt'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 over 7 years@SkorunkaFrantišek it's possible with a few minor changes. I wrote up an answer to explain how.
-
Technetium over 7 yearsIt'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 theAddCommandLine()
extension method. -
Yury Scherbakov over 7 yearsExactly what is needed! Thank you
-
lex82 about 7 yearsIt 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-run
-
Technetium about 7 yearsit 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 about 7 yearsok, sorry. I should have read your answer properly instead of just skimming it ;-)
-
MrJalapeno almost 7 yearsJust 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 almost 7 yearsNevermind, forgot to add
.UseConfiguration(configuration)
to webhostbuilder. -
Bishop about 6 yearsAs an update to dotnetcore 2.0, the
Microsoft.Extensions.Configuration.CommandLine
package doesn't seem necessary now. -
oleksa about 4 yearsSeems that
dotnet run environment=staging
does not work any more. However I can change the environment name using slightly different command line, likedotnet project1.dll --environment Production
whereProject1.dll
is a ASP.NET Core project built for .NET Core 3.1 -
Technetium about 4 years@oleksa: Did you plug in the
AddCommandLine(args)
configuration parsing, or are you trying it without updating yourProgram.cs
file? -
oleksa about 4 yearsI'm using
.AddJsonFile
and.AddEnvironmentVariables()
only. Should I useAddCommandLine
to makeenvironment=development
key works? -
Technetium about 4 yearsYes, 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 about 4 yearsthis works for me -> "dotnet run --launch-profile EnvironmentsSample"
-
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 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 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 almost 2 yearsyou can set the environment from the CLI using "dotnet watch run --environment Development".