0

I have 2 areas with 2 API controllers that have the same AI controller name, but different namespaces and which are configured via 2 different MapHttpRoute() calls with their unique names and routes. They also compile successfully without an issue. However, there are matching action methods in both and when I make a call to one I get back the exception below.

It's clear from the exception message that it's not supported but is there an elegant resolution so I don't have to name my API controllers uniquely despite being in separate namespaces?

Exception:

Multiple types were found that match the controller named 'SomethingApi'. This can happen if the route that services this request ('route-one/api/something/{action}') found multiple controllers defined with the same name but differing namespaces, which is not supported.

Route example:

public static void RegisterApiRoutes(HttpConfiguration config)
{
    // URL: /route-one/api/something/

    config.Routes.MapHttpRoute(
        "RoutOne.Api.Something",
        "route-one/api/something/{action}",
        new { controller = "SomethingApi" }
    );
}

Routes in both namespaces are called from the project's WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        RouteOneAreaRegistration.RegisterApiRoutes(config);
        RouteTwoAreaRegistration.RegisterApiRoutes(config);
    }
}
7
  • Can you show both route registrations for the two separate areas? It appears as though MVC believes you are registering both areas/routes the same... Commented Jan 27, 2014 at 16:24
  • Where do you call the RegisterApiRoutes()? Commented Jan 27, 2014 at 16:25
  • did you tried to specify the area name for MapHttpRoute? Commented Jan 27, 2014 at 16:30
  • @Jason - I can guarantee both routes are unique, they have unique names and routes. Commented Jan 27, 2014 at 16:32
  • @KevinBrechbühl - see amendment for your answer Commented Jan 27, 2014 at 16:34

1 Answer 1

1

It seems to me like the elegant solution here is to name your ApiControllers differently. If they do not do the same thing, reflect that in the controller name. If they do the same thing, then you don't need separate controllers.

Another solution could be to have one controller with both of your different actions, named differently.

It's difficult to gauge why this is not an elegant way to resolve the problem given the abstract nature of your question. Perhaps if you told us what the controllers are named, how they differ, and why they are in separate areas, someone could provide a more appropriate answer for you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, the catch is they do very similar things, just on different business units within the company. So the business unit name difference is reflected in the namespace (and through the MVC area structure). Everything else is named according to the function it performs. I do understand your point though.
Can you make the business unit name or other id a route parameter / action method argument? "api/something/{action}/{businessUnit}"?
I like your thinking but the business unit is a included in the segment earlier on, in fact the business units are reflected by the MVC areas. It just happens in this case two of them perform very similar actions on their respective domains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.