通过命令行在开发中运行.net核心应用程序 [英] run .net core application in development from the command line
问题描述
我知道我可以在Visual Studio或Visual Studio Code中配置一些设置,以在开发模式下启动asp .net核心应用程序.但是我对所有构建工具都有着可怕的经验,到目前为止,从命令行运行是唯一对我有用的工具.
I know I can configure some settings in Visual Studio or Visual Studio Code to launch my asp .net core application in development mode. But I've had a horrible experience with all the build tools, and so far running from the command line is the only one working for me.
我在命令行中遇到的最大问题是使其在开发"模式下运行.据我了解,这只能通过在dotnet watch run
之前键入set ASPNETCORE_ENVIRONMENT=Development
或设置系统范围的环境变量来完成.必须有另一种方式,例如通过文件或其他方式.
The big problem I have with the command line is getting it to run in Development mode. If I understand, this can only be done by typing set ASPNETCORE_ENVIRONMENT=Development
before dotnet watch run
or by setting system wide environment variables. There's gotta be another way, like through a file or something.
如何从命令行在开发模式下运行,因此可以将其包含在Aurelia项目的package.json
中?
How can I run in Development mode from the command line, so I can include this in say a package.json
of an Aurelia project?
推荐答案
您可以将--environment
参数与dotnet run
一起使用以指定托管环境:
You may use --environment
argument with dotnet run
to specify hosting environment:
dotnet run --environment "Development"
但是,只有在您修改主机构造以使用带有命令行参数作为配置源的配置后,此方法才有效.
But this will work only after you modify host building to use configuration with command line arguments as config source.
- 将依赖项添加到
Microsoft.Extensions.Configuration.CommandLine
包 -
通过为
ConfigurationBuilder
添加AddCommandLine
扩展方法,将基于参数的配置创建添加到main
方法:
- add dependency to
Microsoft.Extensions.Configuration.CommandLine
package add to
main
method the config creation based on arguments by addingAddCommandLine
extension method forConfigurationBuilder
:
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
通过在主机设置过程中添加.UseConfiguration(config)
步骤来使用配置.
use configuration by adding .UseConfiguration(config)
step during host setup.
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
这篇关于通过命令行在开发中运行.net核心应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!