dotnet 运行 OR dotnet watch with development environment from 命令行?
dotnet run OR dotnet watch with development environment from command line?
我正在对 运行 asp.net 核心项目使用 dotnet watch
命令。但是,默认情况下,它选择 Production
作为环境。
我已经尝试了两种选择:
1) > dotnet watch ASPNETCORE_ENVIRONMENT=Development
2) > dotnet run ASPNETCORE_ENVIRONMENT=Development
但它仍然将生产环境作为环境。
注意:在 visual studio 中,环境变量在项目属性中默认设置为 Development 并且 运行ning from visual studio 选择该变量。
问题是:如何使用任一命令行 运行 开发中的 dotnet 核心项目?:
1) dotnet run
2) dotnet watch
ASPNETCORE_ENVIRONMENT
是一个环境变量(和 AFAIK)而不是切换到 dotnet
cli。
所以你要做的是在使用该工具之前设置它:
rem Windows
C:\> set ASPNETCORE_ENVIRONMENT=Development
C:\> dotnet ...
rem Unix
$ export ASPNETCORE_ENVIRONMENT=Development
$ dotnet ...
你也可以在调用时设置变量内联dotnet
:
ASPNETCORE_ENVIRONMENT=Development dotnet run
我发现这非常适合 NPM 脚本,但必须始终在 dotnet
之前调用,例如:
{
...
"scripts": {
"start": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet run",
"watch": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet watch"
},
}
注意:这只适用于OSX或Linux;对于跨平台解决方案,您可以使用 cross-env
:
npm install cross-env -D
然后将脚本更改为:
{
...
"scripts": {
"start": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet run",
"watch": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet watch"
},
}
如果您调整 WebHostBuilder
处理其配置的方式,您 不必 使用环境变量。这只是 dotnet new -t web
的默认值。例如,如果您希望能够将默认环境设置为 "development" instead of production 并便于在命令行中覆盖环境,您可以通过修改正常的 Program.cs
代码来实现这个...
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();
}
...变成这样...
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();
}
这样做,环境变量仍然有效,但您可以在命令行上覆盖它而无需任何第三方依赖项,如下所示:
dotnet run environment=development
dotnet run environment=staging
这其实就是the yeoman generators do.
来自 Building Single Page Applications on ASP.NET Core with JavaScriptServices(已添加样式):
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
感谢@Technetium 的回答,这是 dotnetcore 2.0 的更新,对我来说非常有效:
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://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder?view=aspnetcore-2.0 */
// .UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
}
再次感谢@Technetium
检查文档
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/"
},
}
}
我正在对 运行 asp.net 核心项目使用 dotnet watch
命令。但是,默认情况下,它选择 Production
作为环境。
我已经尝试了两种选择:
1) > dotnet watch ASPNETCORE_ENVIRONMENT=Development
2) > dotnet run ASPNETCORE_ENVIRONMENT=Development
但它仍然将生产环境作为环境。
注意:在 visual studio 中,环境变量在项目属性中默认设置为 Development 并且 运行ning from visual studio 选择该变量。
问题是:如何使用任一命令行 运行 开发中的 dotnet 核心项目?:
1) dotnet run
2) dotnet watch
ASPNETCORE_ENVIRONMENT
是一个环境变量(和 AFAIK)而不是切换到 dotnet
cli。
所以你要做的是在使用该工具之前设置它:
rem Windows
C:\> set ASPNETCORE_ENVIRONMENT=Development
C:\> dotnet ...
rem Unix
$ export ASPNETCORE_ENVIRONMENT=Development
$ dotnet ...
你也可以在调用时设置变量内联dotnet
:
ASPNETCORE_ENVIRONMENT=Development dotnet run
我发现这非常适合 NPM 脚本,但必须始终在 dotnet
之前调用,例如:
{
...
"scripts": {
"start": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet run",
"watch": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet watch"
},
}
注意:这只适用于OSX或Linux;对于跨平台解决方案,您可以使用 cross-env
:
npm install cross-env -D
然后将脚本更改为:
{
...
"scripts": {
"start": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet run",
"watch": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet watch"
},
}
如果您调整 WebHostBuilder
处理其配置的方式,您 不必 使用环境变量。这只是 dotnet new -t web
的默认值。例如,如果您希望能够将默认环境设置为 "development" instead of production 并便于在命令行中覆盖环境,您可以通过修改正常的 Program.cs
代码来实现这个...
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();
}
...变成这样...
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();
}
这样做,环境变量仍然有效,但您可以在命令行上覆盖它而无需任何第三方依赖项,如下所示:
dotnet run environment=development
dotnet run environment=staging
这其实就是the yeoman generators do.
来自 Building Single Page Applications on ASP.NET Core with JavaScriptServices(已添加样式):
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
感谢@Technetium 的回答,这是 dotnetcore 2.0 的更新,对我来说非常有效:
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://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder?view=aspnetcore-2.0 */
// .UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
}
再次感谢@Technetium
检查文档
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/"
},
}
}