mar*_*mnl 5 iis asp.net-core asp.net-core-webapi
应该是我想象的简单场景;有
想在带有 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
目录,也许可以使用该模板重新创建项目,然后在部署时将 Angular 构建转储到该目录中(因为 Angular 项目是在单独的 repo 中开发的)。
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
Run Code Online (Sandbox Code Playgroud)
看到这个问题已经一个月了。希望您已经找到解决方案。@JokiesDing 的评论非常切题。
我的开发和部署方法是将 SPA 应用程序放在 Web API 项目中并利用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");
}
});
}
Run Code Online (Sandbox Code Playgroud)
npm run start
每次构建时都运行,可以将 webapi 指向 SPA 开发服务器。 if (env.IsDevelopment())
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
Run Code Online (Sandbox Code Playgroud)
如需参考,请参阅此入门模板 https://github.com/jasontaylordev/CleanArchitecture
归档时间: |
|
查看次数: |
3177 次 |
最近记录: |