protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
//添加以下代码来禁用ASP.NET MVC自带的路由规则
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.IgnoreRoute("");
//添加以下代码,使ASP.NET MVC能够正确地解析Angular的路由
RouteTable.Routes.MapRoute(
name: "Angular",
url: "{*url}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
此代码将指定启用名为“Angular”的路由,将所有路由请求传递给Angular应用程序运行时的主文件(通常为Index.html)。
imports: [
BrowserModule,
RouterModule.forRoot(routes, { useHash: false })
],
By doing this, your Angular routes will be correctly mapped with ASP.NET MVC and your application will work as intended.