15

I have a basic .NET Core 3.1 Web API project that I've created with several endpoints. I now want to build a client to utilize this API. I've seen examples of projects that had Angular within their Web API project solution.

How can I add an Angular project so that debugging and publishing works? Or should I keep both projects separate?

2 Answers 2

20

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.

To do this manually

  1. Move the angular source code into a new folder inside the web project (not wwwroot). The project templates name the folder "ClientApp".
  2. Add the Microsoft.AspNetCore.SpaServices.Extensions nuget package to the project.
  3. In Startup.cs ConfigureServices, call AddSpaStaticFiles and point to a location where the angular app will build.
services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
  1. In 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:
<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>
Sign up to request clarification or add additional context in comments.

3 Comments

So this doesn't require a wwwroot folder?
Nope, the angular built dist folder is included in the project output, and the configuration in startup ensures that the Angular app is served from that output.
If it is early enough in your development phase, I highly recommend starting with the microsoft project template.
11
  1. create wwwroot in api project if not exists

  2. in startup allow app.UseDefaultFiles(); app.UseStaticFiles();

  3. prod your angular ng build --prod

  4. put you angular dist inside to this wwwroot folder

2 Comments

Downvoted this by mistake, it doesn't let me revert it
@TheOne you can revert your downvote now :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.