You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I spent hours trying to figure out why $count is being ignored with OData in AspNetCore 3.1. I Read so many MS articles and posts and no one has an answer.
It just hit me as to why the URL was getting 404 when pointing it to ".../odata/mystuff" but it worked when calling it as "../mystuff" but this of course returned a flag JSON with no OData info in it. This code is supposed to do the work and change the route to be an odata route, but it doesn't.
services.AddControllers(mvcOptions => mvcOptions.EnableEndpointRouting = true)
.AddOData(opt =>
{
opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null).AddRouteComponents("odata", GetEdmModel());
});
The ONLY way to make $count work and your result be a valid OData result, is to append your action method route with "odata/xxx" like this.
[HttpGet]
[Route("odata/mystuff")]
public IQueryable Get()
{
.... your code
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I spent hours trying to figure out why $count is being ignored with OData in AspNetCore 3.1. I Read so many MS articles and posts and no one has an answer.
It just hit me as to why the URL was getting 404 when pointing it to ".../odata/mystuff" but it worked when calling it as "../mystuff" but this of course returned a flag JSON with no OData info in it. This code is supposed to do the work and change the route to be an odata route, but it doesn't.
services.AddControllers(mvcOptions => mvcOptions.EnableEndpointRouting = true)
.AddOData(opt =>
{
opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null).AddRouteComponents("odata", GetEdmModel());
});
The ONLY way to make $count work and your result be a valid OData result, is to append your action method route with "odata/xxx" like this.
[HttpGet]
[Route("odata/mystuff")]
public IQueryable Get()
{
.... your code
}
I hope this helps others.
Beta Was this translation helpful? Give feedback.
All reactions