简体   繁体   English

如何将 Angular 项目添加到现有 .NET 核心 Web ZDB974238714CA8DE634A7CE1D08 项目?

[英]How do I add an Angular project to an exisiting .NET Core Web API project?

I have a basic .NET Core 3.1 Web API project that I've created with several endpoints.我有一个基本的 .NET Core 3.1 Web API 项目,我用几个端点创建。 I now want to build a client to utilize this API.我现在想构建一个客户端来使用这个 API。 I've seen examples of projects that had Angular within their Web API project solution.我已经看到在 Web API 项目解决方案中包含 Angular 的项目示例。

How can I add an Angular project so that debugging and publishing works?如何添加 Angular 项目以便调试和发布工作? Or should I keep both projects separate?还是我应该将两个项目分开?

Microsoft has an existing project template which will set up a new asp.net core project with angular already configured withing that project if you want to base your solution off that template: dotnet new angular . Microsoft 有一个现有的项目模板,如果您希望基于该模板的解决方案,它将设置一个新的 asp.net 核心项目,其中 angular 已经配置了该项目: dotnet new angular

To do this manually手动执行此操作

  1. Move the angular source code into a new folder inside the web project (not wwwroot ).将 angular 源代码移动到 web 项目(不是wwwroot )内的新文件夹中。 The project templates name the folder "ClientApp".项目模板将文件夹命名为“ClientApp”。
  2. Add the Microsoft.AspNetCore.SpaServices.Extensions nuget package to the project.将 Microsoft.AspNetCore.SpaServices.Extensions nuget package 添加到项目中。
  3. In Startup.cs ConfigureServices , call AddSpaStaticFiles and point to a location where the angular app will build.在 Startup.cs ConfigureServices中,调用 AddSpaStaticFiles 并指向 angular 应用程序将构建的位置。
services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
  1. In Startup.cs Configure在 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. And in the Web Projects .csproj file, you can configure the publish step to build the angular app:在 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>
  1. create wwwroot in api project if not exists如果不存在,则在 api 项目中创建wwwroot

  2. in startup allow app.UseDefaultFiles(); app.UseStaticFiles();在启动时允许app.UseDefaultFiles(); app.UseStaticFiles(); app.UseDefaultFiles(); app.UseStaticFiles();

  3. prod your angular ng build --prod刺激你的 angular ng build --prod

  4. put you angular dist inside to this wwwroot folder把你 angular dist 放到这个wwwroot文件夹里面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How to add .net core Web API project to an existing .NET core (3.1) Web Application project? - How to add .net core Web API project to an existing .NET core (3.1) Web Application project? 如何在ASP.NET Web API项目而不是.NET Core上使用DI - How do I use DI on asp.net web api project not .net core 我应该添加哪个项目.Net Core Identity(Web API 或 Web 客户端)? - Which project should I add .Net Core Identity (Web API or in Web Client)? 将Google身份验证添加到**现有** .net核心2 web api项目 - add google authentication to **Existing** .net core 2 web api project 如何在 .NET Core 项目中引用 .NET Framework 项目? - How do I reference a .NET Framework project in a .NET Core project? 如何从 .net 核心 web Z8A5DA52ED12644721AAZ 项目中删除虚拟添加的 angular 文件 - How to remove the virtually added angular files from the .net core web api project 如何在 .NET 核心 5 Web ZDB974238714CA8DE634A7CE1D083A1 项目中自动格式化 API 响应? - How to autoformat API response in .NET core 5 Web API project? ASP.NET核心web API项目中如何添加使用DBContecxt的后台服务 - How to add background service which uses DBContecxt in ASP.NET core web API project 我需要创建一个我的 .Net Core 项目可以连接到的 .Net Web API 项目,但我使用的是 Mac - I need to create a .Net Web API project that my .Net Core project can connect to, but I'm on a Mac 如何设置 ASP.Net 3.0 Core Web API 项目以使用 AutoFac 和 NLog? - How do you setup an ASP.Net 3.0 Core Web API project to use AutoFac and NLog?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM