首页 > 解决方案 > New route re-writes URL to default route, SignalR stops connecting

问题描述

I've attempted to add a route in RouteConfig.cs called 'CustRoute', the only other route present is the default route.

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "CustRoute",
                url: "{cust}/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { cust = new CustConstraint() }
            ).RouteHandler = new CustMvcRouteHandler();

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

The URL reverts back to the default one when loading the page. The page loads but I cannot connect to SignalR either. For example, if I go to www.example.com/Cust/Home it will change the URL to www.example.com/Home on loading the page.

标签: angularjsasp.net-mvcsignalr

解决方案


经过进一步调查,我解决了这个问题,它是ngRoute在 AngularJS 代码中导致问题的,因为元素中的 HTML<base>标记在我的 URL 中<head>没有考虑。/Cust/

ngRoute 使用<base>标签来路由 URL(信息归功于这个答案)。

我通过更改<base href="@(Request.ApplicationPath)"/><base href="@(Request.ApplicationPath + "/" + custName)"/>.


推荐阅读