通过命令行在开发中运行.net核心应用程序 [英] run .net core application in development from the command line

查看:139
本文介绍了通过命令行在开发中运行.net核心应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在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 adding AddCommandLine extension method for ConfigurationBuilder :

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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆