具有多个名称空间的ODataConventionModelBuilder

具有多个名称空间的ODataConventionModelBuilder,odata,Odata,这要么非常直截了当,要么相对容易回答。我有以下代码来设置我的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

这要么非常直截了当,要么相对容易回答。我有以下代码来设置我的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");
//OData
var builder=新的ODataConventionModelBuilder();
//OData实体集。。
建筑商实体集(“账簿”);
建筑商实体集(“货架”);
//绑定函数..必须位于表控制器上。。。
builder.Namespace=“BookService”;
builder.EntityType().Collection
.功能(“最新”)
.Returns();
builder.Namespace=“ShelfService”;
builder.EntityType()
.行动(“最近的行动”);
…但问题是,当应用程序启动时,所有内容都是针对
ShelfService
路由的,而不是从
BookService.MostRecent
ShelfService.NearestEmptyShelf
访问的第一个函数


我确信其他人在为他们的OData控制器创建服务(操作/函数)时遇到了这个特殊问题。但我只是想得到一个明确的答案,即在OData路由集合中是否可以有多个名称空间?

您正在覆盖
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()                
    );
//预订OData端点
var book_builder=新ODataConventionModelBuilder();
//Book OData实体集。。
账簿生成器实体集(“账簿”);
//书籍装订功能..必须位于表格控制器上。。。
book_builder.Namespace=“BookService”;
book_builder.EntityType()集合
.功能(“最新”)
.Returns();
//书籍配置
config.MapODataServiceRoute(
routeName:“小田书”,
routePrefix:“书”,
模型:book_builder.GetEdmModel()
);
//陆架OData端点
var shelf_builder=新ODataConventionModelBuilder();
//工具架OData实体集
工具架构建器实体集(“工具架”);
//工具架绑定函数..必须位于表控制器上。。。
shelf_builder.Namespace=“ShelfService”;
shelf_builder.EntityType()
.行动(“最近的行动”);
.Returns()
//货架配置
config.MapODataServiceRoute(
routeName:“小田-货架”,
routePrefix:“货架”,
模型:shelf_builder.GetEdmModel()
);

我已经有一段时间没有实现这个机制了,但是您可能必须覆盖
AttributeRoutingConvention
,才能使用上述方法在多个名称空间/控制器中使用绑定函数。我知道我在某个时候遇到了一个小问题,最终为
公共类CustomAttributeRoutingConvention:AttributeRoutingConvention
找到了一个解决堆栈溢出的好方法,它利用
公共静态类HttpConfigExt
提供了一个
CustomMapODataServiceRoute
来解决这个问题因为这个问题已经得到了回答,但我最近遇到了这个问题,并找到了另一个解决方案,所以就这样

...
builder.Namespace = "Namespace_A"; // This would be the default namespace
...
function = builder.EntityType<EntityA>()
    .Collection
    .Function("FunctionInNamespace_B")
    .ReturnsCollection<EntityB>();
function.Namespace = "Namespace_B";
。。。
builder.Namespace=“Namespace\u A”//这将是默认名称空间
...
function=builder.EntityType()
收集
.功能(“FunctionInNamespace_B”)
.returns集合();
function.Namespace=“Namespace_B”;

绝对简单,效果很好。

谢谢!我来试试看怎么样