防止WebAPI OData控制器名称发生冲突?

时间:2013-04-26 12:29:57

标签: asp.net-web-api odata

我在Azure中托管了一个WebAPI项目,该项目为移动应用程序提供JSON Web服务。我现在想要添加OData服务以向业务用户提供数据。在一个Web角色中托管两个单独的WebAPI项目看起来很困难,所以我试图让两个服务在一个项目中运行。

我遇到的问题是两者都使用相同的Model类,因此它们都期望相同的Controller类名,例如ProductsController的。如果我尝试将控制器放入不同的命名空间,我会收到错误:

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

由于我无法在MapODataRoute中指定名称空间,因此我无法消除两个控制器的歧义。谁有人建议解决方案?

更新1

配置的一些细节。 OData配置为:

        config.EnableQuerySupport();

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

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

和WebAPI RouteConfig是:

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

1 个答案:

答案 0 :(得分:1)

不幸的是,WebAPI不支持“开箱即用”的领域,比如MVC。您需要替换DefaultHttpControllerSelector。详细信息位于link

此外,您可以尝试使用AttributeRouting.WebApi nuget包(http://attributerouting.net)。此功能已包含在WebAPI路线图中。

相关问题