3

I have a routing problem with MVC and WebAPI controllers.

The error is:

Multiple types were found that match the controller named 'Log'. This can happen if the route that services this request ('api/{controller}/{action}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported. The request for 'Log' has found the following matching controllers: MyNamespace.Controllers.LogController MyNamespace.Controllers.WebAPI.LogController

My routing is simple:

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

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

I have tried adding an additional route for WebAPI like:

routes.MapHttpRoute(
    name: "API",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Along with adding a namespace for the MVC route:

namespaces: new[] { "MyNamespace.Controllers" }

But I still receive the same error. I cannot see a way to specify the namespace for the WebAPI route.

There is a well-upvoted duplicate but that is specifically about MVC controllers. The answers talk about adding a namespace as above, which has not helped here. In my case the routing to the MVC controller works fine, and it is only the WebAPI controller where I have the problem.

The strange thing is, all my other WebAPI controllers have the same name as MVC controllers but this is the only one where I run into the error.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
  • 1
    I'd strongly recommend renaming the controller, in the long run it's just safer and easier to work with anyway. – DavidG May 13 '19 at 13:57
  • @DavidG that does seem the sensible option going forward. But I'm interested in understanding why this is happening though, particularly when it only affects one controller and other duplicate names are fine. – Owen Pauling May 13 '19 at 14:01
  • 1
    What if you put namespace constraint in the API route? like `namespaces: new[] { "MyNamespace.Controllers.WebAPI" }`. Your API controllers are still in the namespace of `MyNamespace.Controllers` if you specify them at the MVC level. – Craig H May 13 '19 at 14:27
  • 1
    @CraigH unless I'm missing something there's no parameter for the namespace in MapHttpRoute (as opposed to MapRoute). – Owen Pauling May 13 '19 at 14:41
  • @OwenPauling have you resolve this? also facing same issue – Mary May 20 '20 at 12:46
  • @csharpQ I don't believe I ever found the reason why this happened, and ended up renaming the controller as per DavidG's recommendation above – Owen Pauling May 21 '20 at 11:19

1 Answers1

3

If you changed the name of the project, you must delete the files in the bin folder with the old project name.

melek
  • 31
  • 2
  • 1
    Welcome to SO! This is a comment rather than an answer, wait to win some more reputation to do them. Anyhow, the OP is not mentioning any project name change... – David García Bodego Dec 28 '19 at 09:17