Webapi odata extends with entity functions

I have an odata product controller and an Odata Product Category controller.
They both use entity framework entities and have navigation methods used to extend odata.
The extension for both works fine.
Now I have added the stored procedure to the entity infrastructure to manipulate the data returned from the database and still return the β€œProduct” record.
I set the type of the function return function of the stored procedure of the object to "Product" and created a new function in the Product Odata controller to call the entity function and return the "Product".
I can call the function from the url and this returns the Product / json object correctly.
Now I need to call the extension on the URL to get the Product Category object, but that fails.

I looked through this article, but it is based on non-essential models. My entities are correct and working fine.
http://beyondtheduck.com/projecting-and-the-odata-expand-query-option-possible-at-last-kinda/

+2
source share
2 answers

Here is the code I used to solve the problem.
In no case is this the "correct" code.
For example: ODataQueryOptions.Top/Skip will be null if used in an Action that contains ODataActionParameters.
Will the ODataActionParameters parameters contain the Up / Skip parameter as the parameter? Very strange. So I added both in the hope that Microsoft or someone else could fix this problem in the future.

Controller:

[HttpPost] [EnableQuery] public PageResult<SomeObject> SomeFunction(ODataQueryOptions<SomeObject> options, ODataActionParameters parameters) { // Get the paging settings from ODataActionParameters since they are not shown on the ODataQueryOptions. Maybe there will be some fix for this in the future. int pageSize = (int)parameters["pageSize"]; int take = (int)parameters["take"]; int skip = (int)parameters["skip"]; int page = (int)parameters["page"]; // Apply page size settings ODataQuerySettings settings = new ODataQuerySettings(); // Create a temp result set to hold the results from the stored procedure var tempResults = db.SomeStoredProc().ToList(); // ToList is required to get the "real" total count before paging // Apply the query options. For now this is only needed to get the correct count since the options does not seem to contain the TOP / SKIP when using OData parameters. IQueryable results = options.ApplyTo(tempResults.AsQueryable(), settings); // This was needed for custom paging. EXAMPLE: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options return new PageResult<SomeObject>(tempResults.Skip(skip).Take(take), Request.ODataProperties().NextLink, Request.ODataProperties().TotalCount); } 

Then WebApiConfig:

 var SomeFunction = builder.Entity<SomeObject>().Collection.Action("SomeFunction"); SomeFunction.Parameter<int>("take"); SomeFunction.Parameter<int>("skip"); SomeFunction.Parameter<int>("page"); SomeFunction.Parameter<int>("pageSize"); SomeFunction.ReturnsCollectionFromEntitySet<SomeObject>("SomeObjects"); 
+1
source

According to your description, it seems that you need to add the [EnableQuery] attribute to the controller method for the stored procedure.

The following implementation works for me:

In WebApiConfig.cs :

 builder.EntityType<Product>().Function("SomeFunction").ReturnsFromEntitySet<Product>("Products"); 

In ProductsController.cs :

 [HttpGet] [EnableQuery] public IHttpActionResult SomeFunction() { return Ok(products.FirstOrDefault(c => c.ID == 1)); } 

In browser:

 GET http://localhost:54017/Products(1)/Default.SomeFunction()?$expand=Categories 

gives

 { @odata.context: "http://localhost:54017/$metadata#Products", value: [ { ID: 1, Name: "Some", Categories: [ { ID: 1, Name: "Some" } ] } ] } 

Updated 10.22.2014:

I changed the attached code and attached it below. Will you try if this works?

 [HttpPost] [EnableQuery(PageSize=10)] public IHttpActionResult SomeFunction() { var results = db.SomeStoredProc().ToList(); return Ok(results); } 

A similar feature worked in my tests. The reason for this is because the OData Web API automatically handles $skip , $top and paging. You do not need to worry about applying them to your result. The request parameters from the client will be applied to the entire set returned by you.

+1
source

All Articles