4
votes

I have a WebAPI project hosted in Azure which provides JSON web service for a mobile app. I now want to add an OData service to provide data to business users. It looks difficult to to get two separate WebAPI projects hosted within one Web Role so I am trying to get both services running within one project.

The problem I have is that the same Model classes are used for both so that they both expect the same Controller class name, e.g. ProductsController. If I try to put the controllers into different namespaces I get an error:

Multiple types were found that match the controller named 'Products'.
This can happen if the route that services this request ('odata/{*odataPath}')
found multiple controllers defined with the same name but differing namespaces,
which is not supported. The request for 'Products' has found the following
matching controllers: 
MyProj.Controllers.OData.ProductsController
MyProj.Controllers.ProductsController

As I cannot specify namespaces in MapODataRoute I am unable to disambigufy the two controllers. Can anyone suggest a solution?

Update 1

Some details on the configuration. The OData configuration is:

        config.EnableQuerySupport();

        ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.EntitySet<Product>("Products");

        Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
        config.Routes.MapODataRoute("ODataRoute", "odata", model);

and the WebAPI RouteConfig is:

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
1
have you found a solution for this? I have the exact same issue. - Martijn
Unfortunately I had to resort to using 'O' as a prefix for all the OData controllers to uniquely name them. - Jason Steele

1 Answers

1
votes

Unfortunately, WebAPI does not support areas "out of the box", like MVC. You'll need to replace DefaultHttpControllerSelector. The details are in the link.

Moreover, you can give a try to AttributeRouting.WebApi nuget package (http://attributerouting.net). This feature is already included in WebAPI roadmap.