使用 Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0 时,“dotnet run”与“npm start”没有什么不同吗?

ras*_*asx 2 visual-studio-code asp.net-core

[docs]*.csproj生成的文件似乎将这些命令呈现为相同的:和。dotnet new angularMicrosoft.DotNet.Web.Spa.ProjectTemplates::2.0.0dotnet runnpm start

每当我打算从 Visual Studio Code 运行 SPAASP.NET Core 后端时,我都需要启动(按 F5)。运行dotnet run 不会同时运行

这种情况是设计使然吗?我什至不应该dotnet run在 VS Code 中使用吗?

Tao*_*hou 5

对于dotnet runnpm start,它们完全不同。dotnet run用于启动.Net Core项目,npm start用于启动Angular项目。

Pressing F5因为将同时启动Core和项目的原因Angular是,当从 启动时VS CodeEnvironemntis Development, thenStartup.Configure将运行以下代码:

spa.UseAngularCliServer(npmScript: "start");
Run Code Online (Sandbox Code Playgroud)

UseAngularCliServer会为你调用StartAngularCliServerAsynclauchAngular项目,该项目对应于run npm start

如果您想从启动Angular并进行项目,有两种选择供您选择 Coredotnet run

  • SET ASPNETCORE_Environment=Development在 Windows 操作系统中从 CommandLin运行以及export ASPNETCORE_Environment=Development在 Linux 或 MacOS 中运行
  • 删除if (env.IsDevelopment())启动中的.

     app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501
    
            spa.Options.SourcePath = "ClientApp";
    
            // if (env.IsDevelopment())
            // {
            //     spa.UseAngularCliServer(npmScript: "start");
            // }
            spa.UseAngularCliServer(npmScript: "start");
        });
    
    Run Code Online (Sandbox Code Playgroud)