Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 Multiple Types Matching Controller

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.

like image 886
tcode Avatar asked Jun 13 '26 22:06

tcode


1 Answers

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" }
);
like image 133
Wojtek Avatar answered Jun 16 '26 21:06

Wojtek