Question

I know there are lots of questions and posts about this particular error on Stackoverflow, however, anything I seem to try still does not fix the problem.

I have an MVC 5 web application which has two Account Controllers, one in the default Controller folder, the other in an Area, like so

  • MyApp.UI.Areas.Admin.Controllers.AccountController
  • MyApp.UI.Controllers.AccountController

When I try to hit the Login Action inside PCF360.UI.Controllers.AccountController with this Actionlink

@Html.ActionLink("Log in", "Login", "Account", new { area = "" }, null)

I get the following error

Multiple types were found that match the controller named 'Account'. The request for 'Account' has found the following matching controllers: PCF360.UI.Areas.Admin.Controllers.AccountController PCF360.UI.Controllers.AccountController

Inside Areas --> Admin --> AdminAreaRegistration

I modified the context.MapRoute like so

context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                new[] { "MyApp.UI.Areas.Admin.Controllers" }
            );

The MapRoute inside App_Start --> RouteConfig looks like this

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

Does anyone see why I am getting this error?

Thanks for your help.

Was it helpful?

Solution

Try defining namespace in RouteConfig:

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

OTHER TIPS

I worked on a project like yours.

My Admin Area Registration is:

   context.MapRoute(
            "admin_default",
            "admin/{controller}/{action}/{id}",
            new { Controller="Home", action = "Index", id = UrlParameter.Optional }
        );

And my RouteConfig is:

   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Maybe configuring your AdminAreaRegistration and RouteConfig like this, can solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top