我想象应该是简单的场景;有
- Angular 8 SPA
- ASP .NET Core 3.1 Web API
想在Windows Server上部署IIS的已通读:使用 IIS 在 Windows 上托管 ASP.NET Core https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1
想要使用推荐的默认进程内托管模型
但是如何将 Angular SPA 和 .net core Web api 结合起来呢?我可以让 Web api 在 IIS 中的新站点下正常运行,从发布输出提供文件,但将我的 SPA 放在哪里?
- 将 Angular 构建与发布输出混合在一起?
- 为 Web API 创建子应用程序,但 IIS 然后添加一个别名,所以我的路由如下
api/blah
become alias/api/blah
- 使用其他主机模型和代理来运行在不同端口上的 Kestrel 服务器?
- 某些 wwwroot 目录要重定向到,因此导航到网站可以从那里提供 SPA 服务吗?
我看看如果使用 Angular 模板创建解决方案,它会创建一个ClientApps/dist
dir,也许可以使用该模板重新创建项目,然后在部署时将 Angular 构建转储到该目录中(因为 Angular 项目是在单独的存储库中开发的)。
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
看到这个问题已经一个月了。希望您已经找到解决方案。 @JokiesDing 的评论非常切题。
我的开发和部署方法是将 SPA 应用程序放在 Web API 项目中并利用Microsoft.AspNetCore.SpaServices.Extensions
。步骤如下:
- Create an Angular Application and put it inside your application directory. For example, I usually create an ClientApp Folder inside the project and place my angular app inside.
- Install
Microsoft.AspNetCore.SpaServices.Extensions
来自努吉特
- 去你的
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 https://github.com/jasontaylordev/CleanArchitecture
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)