Angular2+MVC路由:回退后路由失败
Angular 2 + MVC routing: routing fails after fallback
我正在构建一个 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";
Angular路由是:
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";
应用已加载,但 Angular 路径:'' 与 StartpageComponent 一起显示在 Url /Architecture/ 中。
我错过了什么?
编辑:同样的技术在 AngularJS
中对我有用
好的,我知道了:
运行 本地主机上的应用程序,设置基本参考:
document.write('<base href="' + document.location.href + '" />');
当我刷新页面时,导致基础为 /Architecture。
将其更改为:
<base href="/" />
解决了。
我正在构建一个 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";
Angular路由是:
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";
应用已加载,但 Angular 路径:'' 与 StartpageComponent 一起显示在 Url /Architecture/ 中。
我错过了什么?
编辑:同样的技术在 AngularJS
好的,我知道了:
运行 本地主机上的应用程序,设置基本参考:
document.write('<base href="' + document.location.href + '" />');
当我刷新页面时,导致基础为 /Architecture。
将其更改为:
<base href="/" />
解决了。