[英]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手动执行此操作
wwwroot
).wwwroot
)内的新文件夹中。 The project templates name the folder "ClientApp".ConfigureServices
, call AddSpaStaticFiles and point to a location where the angular app will build.ConfigureServices
中,调用 AddSpaStaticFiles 并指向 angular 应用程序将构建的位置。services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
Configure
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");
}
});
.csproj
file, you can configure the publish step to build the angular app:.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>
create wwwroot
in api project if not exists如果不存在,则在 api 项目中创建
wwwroot
in startup allow app.UseDefaultFiles(); app.UseStaticFiles();
在启动时允许
app.UseDefaultFiles(); app.UseStaticFiles();
app.UseDefaultFiles(); app.UseStaticFiles();
prod your angular ng build --prod
刺激你的 angular
ng build --prod
put you angular dist inside to this wwwroot
folder把你 angular dist 放到这个
wwwroot
文件夹里面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.