"@odata.count" is not show in the return result even after using $count=true.
odata is returning a list of object instead of in the odata format:
this:
[
{"id":"e313596c-8fe9-4db6-b7cb-84f2e059447b","name":"Cody Allen","score":130},{"id":"349a4642-82bd-440f-a05b-ed27ed60cade","name":"Viral Pandya","score":140},{"id":"44fe2696-5285-470f-8b4c-ad983e5241b4","name":"student3","score":120},
{"id":"f2e9d6b7-6825-435b-a86a-37ac89ef77cb","name":"student4","score":150},
{"id":"3a4982f3-a9f3-4bc7-8886-315e7082fd59","name":"student5","score":100}
]
instead of this:
{
"@odata.context":"https://localhost:44372/odata/$metadata#Students",
"@odata.count":5,
"value":[
{"Id":"445bb0cf-efe8-4420-af43-75b37d81dc13","Name":"Cody Allen","Score":130},{"Id":"ec9acefd-1b38-4a0e-aba0-de9949f6690f","Name":"Viral Pandya","Score":140},{"Id":"675ba6a5-c799-4df6-a820-15cb274c33a1","Name":"student3","Score":120},
{"Id":"6fec0f6c-60b6-4624-ad11-38443e0e077f","Name":"student4","Score":150},
{"Id":"a6eeeeb1-8d55-4418-9ab8-4df448a6e1f0","Name":"student5","Score":100}
]}
I did not know the consequences of this until I am not able to work with the paging javascript/library in github. I always think it is ok to not having the @odata.context or the value and other tag, but it is not. It is important. After a few round of search , I found the solution, oData is actually not very flexible. for example:
in the configure function on startup.cs you can change the route to be
routeBuilder.MapODataServiceRoute("odata", "api", model);
and you can happily create some searching like this:
https://localhost:44339/api/applications?$filter=id eq 3fa85f64-5717-4562-b3fc-2c963f66afa6
*note: do not try to cast the id to guid for odata filtering, i tried, a lot, and trust me, this is the best way to filter by guid type id.
you will get result with the following url, but in pure json format.
If you doing it with proper odata, you will get the data under value object.
Remember, oData is not something that is very flexible, first point to remember, is to use the odata as route, although you can alter it to support the api route.
Add this to the configure function in Startup.cs :
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Filter().Count().OrderBy().Expand().MaxTop(null);
routeBuilder.MapODataServiceRoute("odata", "odata", model);
routeBuilder.EnableDependencyInjection();
});
add this private function.
private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
builder.EntitySet<Student>("Students");
return builder.GetEdmModel();
}
Student is the your custom class's name.
Students is the controller class name.
Then, ensure you implement the ODataController, with this:
i.e.
public class StudentsController: ODataController
DO NOT put this line [HttpGet] above the method, leave them as this:
[EnableQuery()]
public IEnumerable<Student> Get()
this is a very important note from my testing result, do not put the HttpGet line and must put the EnableQuery.
run this:
https://localhost:44372/odata/students?$count=true&$top=1
and you will get this:
{
"@odata.context":"https://localhost:44372/odata/$metadata#Students",
"@odata.count":5,
"value":[{"Id":"8a6cee16-98a9-40d8-92b6-58d4d5e1ce16","Name":"student4","Score":150}]
}
:D :D
For detailed code: code only
Github code:
https://github.com/HS-1/OdataDemo
https://github.com/HS-1/OdataWebApplication
No comments:
Post a Comment