2014-12-29 99 views
5

這是非常直截了當的,或者它相對容易回答的問題。我有下面的代碼,以建立我的OData路由約定:具有多個命名空間的ODataConventionModelBuilder

// 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"); 

...但是這個問題是應用程序啓動時,一切都被路由對ShelfService而不是第一個功能是從BookService.MostRecent訪問和​​。

我確信其他人在爲其OData控制器創建服務(操作/功能)時遇到了這個特殊問題。但是我只是在確定OData路由集合中是否有多個名稱空間的最終答案之後呢?

回答

0

您正在用builder.Namespace = "ShelfService";覆蓋您的名字空間builder.Namespace = "Bookservice";

要利用你需要的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()     
    ); 

它已經有一段時間,因爲我實現了這個機制,但你可能有覆蓋AttributeRoutingConvention以利用上述方法在多個命名空間/控制器中利用綁定函數。我知道在某個時候我有一個打嗝,最終找到了一個public class CustomAttributeRoutingConvention : AttributeRoutingConvention堆棧溢出的好方法,利用public static class HttpConfigExt提供CustomMapODataServiceRoute來解決這個問題。

+0

謝謝!我會給它一個裂縫,看看它是如何發展 –