asp.net-mvcasp.net-mvc-5controllerasp.net-mvc-areasareas

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


I can not figure this out.

How to solve the problem?

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


Solution

  • According to error image, you may use different namespaces when declaring an area into RegisterArea to avoid naming conflict between default route and area route:

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

    Possible causes from Multiple types were found that match the controller name error:

    1) Using same controller name with different areas (this is likely your current issue),

    2) Renaming project namespace/assembly name (delete old project name DLL file inside /bin directory then clean and rebuild again),

    3) Conflict between references with same name but different versions (remove older reference then refactor).

    References:

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

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