深度链接时,ASP.NET Core路由会忽略Angular 5路由调用 [英] ASP.NET Core routing ignore Angular 5 routing calls when deep linking

查看:58
本文介绍了深度链接时,ASP.NET Core路由会忽略Angular 5路由调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为API调用运行ASP.NET Core MVC.路由:api/*还可以使用自己的路由模型运行Angular 5.x.

I'm running ASP.NET Core MVC for the API calls. Routing: api/* Also running Angular 5.x with its own routing model.

在localhost(Core运行端口5000和angular 4200)下,一切正常.但是当项目在Azure上发布时,所有内容都将在端口443上运行.

Things work great running localhost (Core runs port 5000 and angular 4200). But when the project is released on Azure everything runs on port 443.

发布应用程序时,深层链接时,所有路由都通过ASPNET Core路由导致404.

When the application is released all routing goes through the ASPNET Core routing causes 404 when deep linking.

如何配置ApplicationBuilder以将Angular路由重定向到Angular?

How do I configure the ApplicationBuilder to redirect the Angular routing to Angular?

链接到API:

  • /api/event/{id}
  • /api/event/{id}/thing

链接到角度:

  • /->(家)
  • /event/{key}
  • /事件/{key}/事物

从主页(/)页面移动并使用角度路由时,用户将重定向到/event/{key}起作用.刷新(F5)时,API路由会启动并返回404(因为api无法理解).

When traveling from the Home (/) page and using the angular routing the user redirects to /event/{key} which works. When refreshing (F5) the API routing kicks in and returns 404 (because the api doesn't understand).

这是解决方法:

    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();
    }

推荐答案

添加 app.UseStatusCodePagesWithReExecute()可以解决路由问题.

Adding app.UseStatusCodePagesWithReExecute() fixed the routing problems.

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();
}

这篇关于深度链接时,ASP.NET Core路由会忽略Angular 5路由调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆