具有多个名称空间的ODataConventionModelBuilder

Mat*_*ull 6 odata-v4

这要么是超级直接的,要么相对容易回答.我有以下代码来设置我的OData路由约定:

// OData
var builder = new ODataConventionModelBuilder();

// OData entity sets..
builder.EntitySet<Book>("Books");
builder.EntitySet<Shelf>("Shelves");

// Bound Function..has to be located on the Tables Controller...
builder.Namespace = "BookService";
builder.EntityType<Table>().Collection
    .Function("MostRecent")
    .Returns<DateTimeOffset>();

builder.Namespace = "ShelfService";
builder.EntityType<Shelf>()
    .Action("NearestEmptyShelf");
Run Code Online (Sandbox Code Playgroud)

...但问题是当应用程序启动时,所有内容都被路由,ShelfService而不是第一个可以从BookService.MostRecent和访问的函数ShelfService.NearestEmptyShelf.

我确定其他人在为OData控制器创建服务(操作/功能)时遇到了这个特殊问题.但我刚刚回答了关于你是否可以在OData Routing Collection中拥有多个命名空间的明确答案?

Pyn*_*ynt 2

您正在使用 覆盖您的命名builder.Namespace = "Bookservice";空间builder.Namespace = "ShelfService";

要利用两个单独的名称空间,您需要两个单独的实例new ODataConventionModelBuilder();

以下为 OData V4

// Book OData Endpoint
var book_builder = new ODataConventionModelBuilder();

// Book OData entity sets..
book_builder.EntitySet<Book>("Books");

// Book Bound Function..has to be located on the Tables Controller...
book_builder.Namespace = "BookService";
book_builder.EntityType<Table>().Collection
    .Function("MostRecent")
    .Returns<DateTimeOffset>();
// Book Config
config.MapODataServiceRoute(
    routeName: "OData - Book",
    routePrefix: "book",
    model: book_builder.GetEdmModel()                
    );

// Shelf OData Endpoint
var shelf_builder = new ODataConventionModelBuilder();

// Shelf OData Entity Sets
shelf_builder.EntitySet<Shelf>("Shelves");

// Shelf Bound Function..has to be located on the Tables Controller...
shelf_builder.Namespace = "ShelfService";
shelf_builder.EntityType<Shelf>()
    .Action("NearestEmptyShelf");
    .Returns<whatever you planned on returning>()
//Shelf Config
config.MapODataServiceRoute(
    routeName: "OData - Shelf",
    routePrefix: "shelf",
    model: shelf_builder.GetEdmModel()                
    );
Run Code Online (Sandbox Code Playgroud)

我实现这个机制已经有一段时间了,但是您可能必须AttributeRoutingConvention使用上述方法进行覆盖才能在多个命名空间/控制器中使用绑定函数。我知道我在某个时候遇到了问题,最终找到了一个关于堆栈溢出的好方法public class CustomAttributeRoutingConvention : AttributeRoutingConvention,利用 apublic static class HttpConfigExt来提供 aCustomMapODataServiceRoute来解决问题。