【发布时间】:2018-04-16 12:40:12
【问题描述】:
我正在为 API 调用运行 ASP.NET Core MVC。路由:api/* 还使用自己的路由模型运行 Angular 5.x。
运行 localhost 运行良好(Core 运行端口 5000 和 angular 4200)。但是当项目在 Azure 上发布时,一切都在端口 443 上运行。
当应用程序发布时,所有路由都经过 ASPNET Core 路由,导致 深度链接时出现 404。
如何配置 ApplicationBuilder 以将 Angular 路由重定向到 Angular?
更新:
API 链接:
- /api/event/{id}
- /api/event/{id}/thing
角度链接:
- / --> (家)
- /event/{key}
- /event/{key}/thing
当从主页 (/) 页面旅行并使用角度路由时,用户会重定向到 /event/{key} ,这很有效。刷新 (F5) 时,API 路由启动并返回 404(因为 api 不理解)。
更新2:
这是解决方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc(); // <-- moved to top (was on bottom)
app.UseStatusCodePagesWithReExecute("/"); // <-- added to redirect to angular
app.UseDefaultFiles();
app.UseStaticFiles();
}
【问题讨论】:
-
您是否设置了单个部署,即您捆绑的角度代码位于您的 api 项目内的文件夹中?如果是这样,如果请求不是 api 请求,您是否有任何管道代码返回 index.html?
-
是的,它是单一部署。我正在研究如何区分 sturtup.cs 中的 api 和 index.html 代码。目前正在阅读blog.nbellocam.me/2016/03/21/routing-angular-2-asp-net-core,但它是旧版本。
-
您需要创建一个自定义中间件,为每个请求执行。如果请求不以 /api/ 开头,则返回 index.html 文件。之后,所有非 api 路由都应该由 Angular 路由器处理。这里有一个很好的解释exceptionnotfound.net/asp-net-core-demystified-middleware。
-
您使用的模板一定是非常过时的。较新的 ASP.NET Core 2.0 模板使用 webpack 捆绑并将 Angular 应用程序作为来自
wwwroot根文件夹的静态文件提供服务,该文件夹始终在 MVC 中间件之前首先注册(除非您更改了没有多大意义的内容) -
@tseng 路由工作正常,除非在深度链接时。
标签: angular asp.net-core