Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 444
JHizzal Avatar asked Jul 14 '20 19:07

JHizzal


People also ask

How does Angular project integrate with NET Core?

To use publish, create your JavaScript project using Visual Studio 2022 version 17.3 or later. In Solution Explorer, right-click the ASP.NET Core project and choose Add > Project Reference. Select the Angular project and choose OK. Right-click the ASP.NET Core project in Solution Explorer and choose Unload Project.

How do I run Angular and web API on the same port?

The idea is that you can't "publish" the API and the Angular app on the same port, but you can use a proxy to connect from the Angular app to the API on the other port. Update: sorry for not answering this long time ago. To deal with the cors issue (in this case) you can use a proxy with the ng serve command.

How does Angular connect to dotnet?

Create ASP.NET Core Web ApplicationClick New >> Project. Select Web >> ASP.NET Core Web Application. Enter your project name and click OK. Select Angular Project and click OK.

How do I connect ASP NET Core to angular?

Summary First, create your ASP.NET Core Web API. To do that just follow the steps below. Select File > New > Project. Select ASP.NET Core Web Application. Name the project AngularDemo to have the same namespace as my project. Click OK. Select ASP.NET Core with Angular and then uncheck Configure for HTTPS.

How do I create a web API with AngularJS?

First, create your ASP.NET Core Web API. To do that just follow the steps below. Select File > New > Project. Select ASP.NET Core Web Application. Name the project AngularDemo to have the same namespace as my project. Click OK. Select ASP.NET Core with Angular and then uncheck Configure for HTTPS.

How do I create an angular project in Visual Studio?

Create and Setup a new Angular using Visual Studio 1 Select File > New > Project. 2 Select ASP.NET Core Web Application. Name the project AngularDemo to have the same namespace as my project. Click OK. 3 Select ASP.NET Core with Angular and then uncheck Configure for HTTPS. 4 Lastly, Click on Create.

Can I use angular and ASP NET in the same project?

I recommend that you set up your Angular project in a completely separate folder and use the build process to move files over if you want to run them in the same Web as your ASP.NET Web API. If you use full Visual Studio, you can use a Web Site project for the Angular project.


Video Answer


2 Answers

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>
like image 158
Chris Danna Avatar answered Oct 29 '22 00:10

Chris Danna


  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

like image 36
mr. pc_coder Avatar answered Oct 28 '22 23:10

mr. pc_coder