run .net core application in development from the command line

久未见 提交于 2019-12-18 09:29:38

问题


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.

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.

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?


回答1:


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.

  • 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();
    
  • use configuration by adding .UseConfiguration(config) step during host setup.

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


来源:https://stackoverflow.com/questions/43412065/run-net-core-application-in-development-from-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!