MVC C# 控制器冲突(站点前端与区域管理员)

MVC C# Controller Conflict (site front with area admin)

我想不通。

如何解决问题?

AdminAreaReistration cs

        public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "CMSAdmin_default",
            "CMSAdmin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

路由配置

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

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

根据错误图片,您可以在将区域声明为 RegisterArea 时使用不同的命名空间,以避免默认路由和区域路由之间的命名冲突:

AdminAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "CMSAdmin_default",
        "CMSAdmin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "cms.site.Areas.CMSAdmin.Controllers" } // Insert area namespace here
    );
}

RouteConfig.cs

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "cms.site.Controllers" } // Insert project namespace here
    );
}

Multiple types were found that match the controller name 错误的可能原因:

1) 在不同区域使用相同的控制器名称(这可能是您当前的问题),

2) 重命名项目namespace/assembly名称(删除/bin目录下的旧项目名称DLL文件然后清理并重新生成),

3) 具有相同名称但不同版本的引用之间存在冲突(删除旧引用然后重构)。

参考文献:

Multiple types were found that match the controller named 'Home'

Having issue with multiple controllers of the same name in my project