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
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
launchSettings.json
With credit to @Technetium 's answer, here's an update to dotnetcore 2.0 that works really well for me:
Thanks again, @Technetium
You don't have to use environment variables if you adjust how the
WebHostBuilder
processes its configuration. This is merely the default fordotnet 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 normalProgram.cs
code from this ...... into something like this ...
Doing this, the environment variables would still work, but you can override it on the command line without any third-party dependencies, like so:
This is actually what the yeoman generators do.
ASPNETCORE_ENVIRONMENT
is an environment variable (and AFAIK) not a switch to thedotnet
cli.So what you would do is set it prior to using the tool:
Source: https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/
Styling added.
You can also set the variable inline when calling
dotnet
:I have found this is great for NPM scripts, but must always be called right before
dotnet
, e.g.:Note: This only works in OS X or Linux; for a cross-platform solution, you can use
cross-env
:Then change the scripts to: