【问题标题】:Angular 2 + MVC routing: routing fails after fallbackAngular 2 + MVC路由:回退后路由失败
【发布时间】:2018-03-24 04:39:40
【问题描述】:

我正在构建一个 Angular 2 应用程序。

使用 Visual Studio 2017 启动项目时,我可以成功路由到区域内的应用程序 (Home2017/Start/Index.cshtml):

  Route toHome2017 = null;
  toHome2017 = routes.MapRoute(
              name: "Default_to_Home2017",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Start", action = "Index", id = UrlParameter.Optional },
              namespaces: new string[] { "BIP2.Areas.Home2017.Controllers" }
            );

            toHome2017.DataTokens["area"] = "Home2017";

角度路由是:

RouterModule.forRoot([

        {
            path: 'Architecture',
            component: ArchitectureComponent,
            pathMatch: 'full'
        },            
        {
            path: '',
            component: StartpageComponent,
            pathMatch: 'full'
        }
    ])
]

当我导航到 /Architecture 时,Angular 路由正在工作。
但是,当我刷新页面并使用以下命令将其重定向到 Home2017/Start/Index 时:

 toHome2017 = routes.MapRoute(
            name: "Default_to_Architecture",
            url: "Architecture",
            defaults: new { controller = "Start", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "BIP2.Areas.Home2017.Controllers" }
          );
            toHome2017.DataTokens["area"] = "Home2017";

应用已加载,带有 StartpageComponent 的 Angular 路径:'' 与 Url /Architecture/ 一起显示。

我错过了什么?



编辑:同样的技术在 AngularJS 中对我有用

【问题讨论】:

    标签: javascript asp.net-mvc angular asp.net-mvc-4 typescript


    【解决方案1】:

    好的,我已经找到了:

    在 localhost 上运行应用,设置基础 ref:

    document.write('<base href="' + document.location.href + '" />');
    

    当我刷新页面时,导致基础为 /Architecture。
    将其更改为简单:

    <base href="/" />
    

    解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多