我正在尝试使用Angular 7应用程序配置MVC 5应用程序的路由,以便它们可以平滑地协同工作,我执行以下操作: 我创建了一个新类:
{
private readonly Func<Uri, bool> _predicate;
public ServerRouteConstraint(Func<Uri, bool> predicate)
{
this._predicate = predicate;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
return this._predicate(httpContext.Request.Url);
}
}
在RouteConfig.cs文件中之后:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// Set a constraint to only use this for routes identified as server-side routes
constraints: new
{
serverRoute = new ServerRouteConstraint(url =>
{
return (url.PathAndQuery.StartsWith("/Account", StringComparison.InvariantCultureIgnoreCase)
|| url.PathAndQuery.StartsWith("/Home", StringComparison.InvariantCultureIgnoreCase)
|| url.PathAndQuery.StartsWith("/Manage", StringComparison.InvariantCultureIgnoreCase)
|| url.PathAndQuery.StartsWith("/", StringComparison.InvariantCultureIgnoreCase));
})
});
// This is a catch-all for when no other routes matched. Let the Angular 2 router take care of it
routes.MapRoute(
name: "angular",
url: "{*url}",
defaults: new { controller = "Modify", action = "Index" } // The view that bootstraps Angular 2
);
它运行良好,因此可以捕获服务器端路由,顺利导航到角度路由。当我在角度模板上并按F5刷新页面时出现问题,它给我404 not found错误。 我认为从来没有遇到过弯角路线。