-
Hi, I'm experimenting with disabling the routing conventions and using only attribute routing. One thing I've been unable to solve is that EntitySet routing convention also adds a I tried adding a new action to my controller with [HttpGet("odata/Documents/$count")]
[EnableQuery]
public async Task<ActionResult<int>> CountDocuments() { } However if I execute {
"error": {
"code": "",
"message": "The query specified in the URI is not valid. The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.",
"details": [],
"innererror": {
"message": "The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.",
"type": "Microsoft.OData.ODataException",
"stacktrace": " at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ValidateSelectExpandOnly(ODataQueryOptions queryOptions)\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)\n at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext actionExecutedContext, Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)"
}
}
}
So, I'm not sure what I need to do to make the $count work using only attribute routing? Thanks for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@sliekens What you added as '[HttpGet("odata/Documents/$count")]' is correct. But, the return type of your OData counts the number of 'Documents' based on the collection of 'Document', so returning an Please try to return collection of |
Beta Was this translation helpful? Give feedback.
-
As @xuzhg says, OData expects the path for /$count to return the collection of documents, so it can apply query options (i.e. $filter). So, you should be able to annotate a single GetDocuments controller method that returns all documents with both |
Beta Was this translation helpful? Give feedback.
As @xuzhg says, OData expects the path for /$count to return the collection of documents, so it can apply query options (i.e. $filter). So, you should be able to annotate a single GetDocuments controller method that returns all documents with both
[HttpGet("odata/Documents")]
and[HttpGet("odata/Documents/$count")]