应该是我想象的简单场景;有
想用IIS部署在Windows Server上的请通读:Host ASP.NET Core on Windows with IIS
想要使用推荐的默认进程内托管模型
但是如何结合 Angular SPA 和 .net core web api?我可以让 web api 在 IIS 中的新站点下正常运行,从发布输出提供文件,但是我的 SPA 放在哪里?
api/blah
成为alias/api/blah
我看到如果使用 Angular 模板来创建解决方案,它会创建一个
ClientApps/dist
dir,也许可以使用该模板重新创建项目,然后在部署时将 Angular 构建转储到该目录中(因为 Angular 项目是在单独的 repo 中开发的)。 // In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
最佳答案
看到这个问题已经一个月了。希望你已经找到了解决办法。 @JokiesDing 评论非常重要。
我的开发和部署方法是在 Web API 项目中使用 SPA 应用程序并利用 Microsoft.AspNetCore.SpaServices.Extensions
.以下是步骤:
Microsoft.AspNetCore.SpaServices.Extensions
来自 Nuget Startup.cs
并添加扩展方法 public void ConfigureServices(IServiceCollection services)
{
//... whatever you have in your ConfigureServices method...
//add this
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//... whatever you have in your configure method...
//add this
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
npm run start
每次构建时,您都可以将 webapi 指向 SPA 开发服务器。 if (env.IsDevelopment())
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
有关引用,请参阅此入门模板https://github.com/jasontaylordev/CleanArchitecture
关于asp.net-core - 如何在 IIS 上使用 ASP.NET Core 3.1 API 部署 Angular SPA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62055847/