如何将Angular项目添加到现有的.NET Core Web API项目中?

希扎尔

我有一个使用多个端点创建的基本.NET Core 3.1 Web API项目。我现在想构建一个客户端来利用此API。我已经看到了在其Web API项目解决方案中包含Angular的项目示例。

如何添加Angular项目以便调试和发布?还是应该将两个项目分开?

克里斯

Microsoft有一个现有的项目模板,如果您想基于该模板创建解决方案,它将使用该项目设置一个新的asp.net核心项目dotnet new angular

手动执行

  1. 将有角度的源代码移动到Web项目内的新文件夹中(不是wwwroot)。项目模板将文件夹命名为“ ClientApp”。
  2. 将Microsoft.AspNetCore.SpaServices.Extensions nuget包添加到项目中。
  3. 在Startup.cs中ConfigureServices,调用AddSpaStaticFiles并指向角度应用程序将构建的位置。
services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
  1. 在Startup.cs中 Configure
app.UseStaticFiles();
if (!env.IsDevelopment())
{
    app.UseSpaStaticFiles();
}

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.UseProxyToSpaDevelopmentServer("http://localhost:4200");
    }
});
  1. 在Web Projects.csproj文件中,您可以配置发布步骤以构建Angular应用程序:
<PropertyGroup>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
</Target>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将Vue.js添加到现有的ASP .NET Core 2.0 MVC项目中?

将Angular添加到现有的ASP.NET Web窗体项目中?

如何将Web API控制器添加到现有的ASP.NET Core MVC?

如何将Web API添加到现有的ASP.NET MVC(5)Web应用程序项目中?

如何将Web API添加到现有的ASP.NET MVC 4 Web应用程序项目中?

如何将ASP.NET Core身份添加到现有Core mvc项目中?

如何将Web API添加到现有的MVC Hottowel项目

使用 .Net 4 将 MVC 项目添加到现有的 Web 应用程序

如何将Core Data添加到现有的Xcode 9 Swift 4 iOS 11项目中?

如何将Firebase Cloud功能样板添加到现有的Firebase Web项目中?

如何将IdentityServer3添加到现有的基于ASP.Net Identity的项目中?

如何将Bridge.net添加到我现有的c#项目中?

在ASP.NET Core 3.0 Web API项目中使用WebApiContrib.Core.Formatter.Csv

.NET Core API项目中的分页

在 .NET CORE 3.1 Web API 项目中配置 nLog 的自定义布局渲染器

在 Web 窗体项目中使用 .Net Core 项目

我应该在哪个项目中添加 .Net Core Identity(Web API 或在 Web Client 中)?

如何将React.js添加到现有的.NET Core

具有CLR .Net Core库(C ++ / CLI)的Web .Net Core 3.1项目中的BadImageFormat

单元测试-控制器未从ASP.NET Core Web API项目中的模拟IConfiguration中获取值

如何将Web Animations API polyfill添加到使用Angular CLI创建的Angular 2项目中

使用 .Net Core web api 将控制台应用程序项目添加到解决方案

更新 ASP.NET Web API 项目中插入的记录

将web.config文件添加到Asp.Net Core项目

Angular-cli 项目,托管在 Visual Studio Core 2.2 Web Api 项目中,无法编译,随机

将Web Forms添加到现有的MVC项目

将ASP.NET MVC区域添加到ASP.NET Web窗体现有项目

如何将libgdx添加到现有的android studio项目中?

如何将监视模块添加到现有的Android Studio项目中?