ODataConventionModelBuilder with multiple namespaces

It is either super direct or relatively easy to answer. I have the following code to configure my OData routing rules:

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

... But the problem is that when the application starts, everything is routed using ShelfService , and not from the first function available from BookService.MostRecent and ShelfService.NearestEmptyShelf .

I am sure that other users have encountered this problem when creating services (actions / functions) for their OData controllers. But I immediately after the final answer about whether you can have multiple namespaces in the OData route collection?

+5
source share
1 answer

You are rewriting the namespace builder.Namespace = "Bookservice"; using builder.Namespace = "ShelfService"; .

To use two separate namespaces, you will need two separate instances of new ODataConventionModelBuilder();

Below is a description of 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() ); 

It has been a while since I implemented this mechanism, but you may have to rewrite AttributeRoutingConvention to use related functions in multiple namespaces / controllers using the above method. I know that at some point I had a hiccup and I found a good method for the public class CustomAttributeRoutingConvention : AttributeRoutingConvention that used the public static class HttpConfigExt to provide CustomMapODataServiceRoute to fix this problem.

0
source

Source: https://habr.com/ru/post/1210045/


All Articles