Reputation: 1058
I have a get endpoint request - endpt{id} which retrieves the table row based on the passed integer
id. So by default all the columns are returned from the table.
I want to return only a subset of total columns of table. So, I am trying to use oData $select
I query as
{baseurl}/endpoint/3?$select=tag
where Tag is one of the column name.
In my startup.cs file, Confiure method , registered oData this way..
My Get Endpoint..
[HttpGet]
[EnableQuery]
[ODataRoute("endpoint({id})")]
[Route("endpt", Name = "endpt", Order = 0)]
[Route("endpt/{id}", Name = "endpt", Order = 1)]
[ProducesResponseType(typeof(APIResponseModel<List<EndPoints>>), StatusCodes.Status200OK)]
public APIResponseViewModel<List<EndPoints>> GetEndPoints(int limit, [FromODataUri] int id)
{
List<EndPoints> ListOfEndPoints;
try
{
if (id.Equals(0))
{
ListOfEndPoints = db.EndPoints.ToList();
}
else
{
ListOfEndPoints = db.EndPoints.Where(x => x.ID.Equals(id)).ToList();
}
}
catch (Exception e)
{
//catch exp
}
if (limit == 0)
{
this.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
return new APIResponseModel<List<EndPoints>>
{
StatusCode = (int)HttpStatusCode.OK,
Message = "Success",
Result = ListOfEndPoints,
Count = ListOfEndPoints.Count
};
}
else
{
this.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
return new APIResponseModel<List<EndPoints>>
{
StatusCode = (int)HttpStatusCode.OK,
Message = "Success",
Result = ListOfEndPoints.Take(limit).ToList(),
Count = ListOfEndPoints.Count
};
}
}
Breakpoint hits, but in id
I get only the ID that I passed - 3. select tag doesn't comes up, and from the implmented logic, it returns complete row matching that id with all the columns.
But at client side I recieve an error from oDataUriParser..
{
"Message": "The query specified in the URI is not valid. Could not find a property named 'tag' on type 'project'.",
"ExceptionMessage": "Could not find a property named 'tag' on type 'Projects'.",
"ExceptionType": "Microsoft.OData.ODataException",
"StackTrace": " at Microsoft.OData.UriParser.SelectPathSegmentTokenBinder.ConvertNonTypeTokenToSegment(PathSegmentToken tokenIn, IEdmModel model, IEdmStructuredType edmType, ODataUriResolver resolver, BindingState state)\r\n at Microsoft.OData.UriParser.SelectExpandBinder.ProcessSelectTokenPath(PathSegmentToken tokenIn)\r\n at Microsoft.OData.UriParser.SelectExpandBinder.GenerateSelectItem(SelectTermToken tokenIn)\r\n at Microsoft.OData.UriParser.SelectExpandBinder.Bind(ExpandToken expandToken, SelectToken selectToken)\r\n at Microsoft.OData.UriParser.SelectExpandSemanticBinder.Bind(ODataPathInfo odataPathInfo, ExpandToken expandToken, SelectToken selectToken, ODataUriParserConfiguration configuration, BindingState state)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select, String expand, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()\r\n at Microsoft.AspNet.OData.Query.SelectExpandQueryOption.get_SelectExpandClause()\r\n at Microsoft.AspNet.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequest request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.CreateAndValidateQueryOptions(HttpRequest request, ODataQueryContext queryContext)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass1_0.<OnActionExecuted>b__1(ODataQueryContext queryContext)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)"
}
Please help me resolve this, as I don't see any complexity in my URL. but I am not able to find the missing point.
Thanks
Upvotes: 1
Views: 2504
Reputation: 21
You might try changing this:
oDataBuilder.EntitySet<Endpoints>("Endpoints");
to this:
oDataBuilder.EntitySet<Endpoint>("Endpoints");
The idea being that your model is called Endpoint
and your controller is called Endpoints
.
Upvotes: 1