angular - 如何使用 msbuild 从 dotnetcore web-api 项目中触发 "ng build",但将 Angular 项目保存在单独的文件夹中?

标签 angular asp.net-web-api msbuild

我有一个使用 Angular CLI 构建的 Angular 应用程序。我希望我的 web-api 提供 wwwroot 文件夹中的静态文件。我还想将这两个项目很好地分开放在它们自己的文件夹中。这是我的解决方案的结构:

.\angular-app
.\angular-app\angular-cli.json
.\web-api-app
.\web-api-app\wwwroot

鉴于我的应用程序的这种结构,我想要的是配置我的 msbuild 脚本 web-api-app.csproj 以某种方式从 angular-app 中触发“ng build”并将构建输出到 wwwroot 文件夹中。

因此,我将 .angular-cli.json 中的“outDir”参数修改为:“../web-api-app/wwwroot”,这样当我从 angular-app 中调用“ng build”时,webpack将结果输出到 wwwroot 文件夹中。

如何设置我的 MsBuild 脚本,以便在我调用“dotnet build -c Release”时,angular 很好地打包在 wwwroot 文件夹中?

最佳答案

我找到了一种能够从我的 msbuild 中调用 « ng build » 的方法。诀窍是在 DotNetCore 应用程序的根目录中使用一个 npm 脚本,将其“cd”到 Angular 应用程序中,然后调用“ng build”。这些是我采取的步骤: 1)为了能够使用 npm 脚本命令编辑 angular-app 的 package.json 的脚本部分:(这里我还定义了一个启动脚本来轻松启动我的本地开发环境和构建以便能够构建我的脚本 Debug模式)

"scripts": {
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build",
    "build-prd": "ng build --prod --env=prod",
 }

2) 使用 Web-Api 应用程序,在 web-api 应用程序的根目录中添加一个 package.json,其中包含:

"scripts": {
        "install": "cd ../angular-app & npm install",
        "start": "cd ../angular-app & npm start",
        "build": "cd ../angular-app & npm run-script build",
        "build-prd": "cd ../angular-app & npm run-script build-prd"
}

3) 最后,配置您的 web-api MsBuild 脚本以在运行发布构建时调用 ng 构建脚本。因此,将以下目标添加到 csproj 文件中:

<Target Name="EnsureNode">
  <Exec Command="node --version" ContinueOnError="true">
    <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
  </Exec>
  <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
</Target>
<Target Name="ReleaseRunNgBuild" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Release' ">
  <CallTarget Targets="EnsureNode" />
  <Message Importance="high" Text="Install packages..." />
  <Exec Command="npm install" />
  <Message Importance="high" Text="Performing ng build for prd build..." />
  <Exec Command="npm run-script build-prd" />
</Target>

我创建了一个示例应用程序,您可以在其中找到完整的源代码 my github project core-angular-examplethis blog post还详细描述了每个步骤。

关于angular - 如何使用 msbuild 从 dotnetcore web-api 项目中触发 "ng build",但将 Angular 项目保存在单独的文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46919126/

相关文章:

asp.net-core - <EnableDefaultContentItems> 启用哪些内容项?

c++ - 错误 LNK2038 : mismatch detected for '_MSC_VER' : value '1600' doesn't match value '1700' in CppFile1. 对象

带有参数的Angular 5 Component输入函数

asp.net-mvc - Microsoft ASP .NET Web API,MVC 4和SPA体系结构

c# - 使用 (XALM) 构建过程模板在 TFS 2013 中将工具路径设置为 MSBuild 2019

c# - Web API 对服务使用者隐藏字段

c# - 您能否将匿名对象作为 json 发布到 web api 方法,并将 json 对象的属性与 webapi 调用的参数相匹配?

css - 如何从 webpack angular2 应用程序中的 node_modules 导入 CSS

html - 背景大小 : initial not working in IE10 and under

ios - 在 iOS 上为启用 PWA 的应用程序安装到主屏幕