从命令行在开发中运行

从命令行在开发中运行

本文介绍了从命令行在开发中运行 .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) 步骤来使用配置.

    var host = new WebHostBuilder()
       .UseConfiguration(config)
       .UseKestrel()
       .UseContentRoot(Directory.GetCurrentDirectory())
       .UseIISIntegration()
       .UseStartup<Startup>()
       .Build();
    

  • 这篇关于从命令行在开发中运行 .net 核心应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-24 00:38