找到多个与名为“user”的控制器匹配的类型 两个Area 解决办法

项目架构:两个Area 一个 Admin,一个User,两个Area里面都有重名的控制器UserControl,结果报错

通过搜索,网站的一致解决办法为,在路由里面用namespaces限定 如下

在区域Member Admin和最外层的RouteConfig.cs限定

public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Member_default",
                "Member/{controller}/{action}/{id}",
                new { Controller = "Home", action = "index", id = UrlParameter.Optional },
                 namespaces: new string[] {"SSHB.Areas.Member.Controllers"}
            );
        }
public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { Controller = "Home", action = "index", id = UrlParameter.Optional },
                namespaces: new string[] {"SSHB.Areas.Admin.Controllers"}
            );
        }
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "SSHB.WEB.Controllers" }
            );
        }

结果Admin/User/index、User/User/Index正常,/AAA/这种不存在的控制器返回404也正常,但是当访问到/User/index/的时候仍然出现上述错误,花了2个多小时找问题,最后发现必须要在RouteConfig.cs里添加一个参数才可以DataTokens[“UseNamespaceFallback”] = false

最终结果:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces:new string[] {"SSHB.WEB.Controllers"}
            ).DataTokens["UseNamespaceFallback"] = false; 
        }

至此全部正常,有点坑爹,关于DataTokens[“UseNamespaceFallback”] ,默认为true,设置为false,则只在当前设置的命名空间内匹配规则,而不会去到其他的命名空间里查找匹配。

坑爹的地方在于如果不设置DataTokens[“UseNamespaceFallback”] =false,他会去所有的命名空间下查找User,最终在Area下查找到重名的两个控制器,然后报错,不匹配了还报什么错,直接返回404就完事,日