MVC C#Controller Conflict(站点前端区域管理员)

时间:2016-11-17 17:26:07

标签: asp.net-mvc asp.net-mvc-5 controller asp.net-mvc-areas areas

我无法弄清楚这一点。

如何解决问题?

AdminAreaReistration cs

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

RouteConfig

        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 }
        );
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

根据错误图像,您可以在将区域声明为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)重命名项目命名空间/程序集名称(删除/ 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