Enable OData v4 request on WebApi ApiController

I am trying to add OData v4 query support to a method on a controller that inherits from ApiController, not ODataController. Although I have a working OData model in the solution, there are some endpoints that don't actually belong to the model, but the usefulness of the query would be useful.

I have seen some articles suggesting that I simply return IQueryable and use EnableQuery.

Here is my sample code:

public class TestController : ApiController
{
    [HttpGet]    
    [EnableQuery]
    public IQueryable<TestObject> Events()
    {
        var result = new[] { new TestObject { Id = "1" } }.AsQueryable();
        return result;
    }
}

public class TestObject
{
    public string Id { get; set; }
}

, , // - 406, -, OData, , , OData. (, , ) , , .

- , , ?

406?

EDIT:

, , , ODataMediaTypeFormatter .

.

, , WebApi - , 406, .

+4
1

"" , [EnableQuery].

  • ODataQueryContext, ( ), - .
  • HttpRequestMessage Get URL- (Request.RequestUri.AbsoluteUri).
  • ODataQueryOptions <yourEntity> , .
  • ApplyTo . AsQueryable(), IQueryable <yourEntity> .

. , IQueryable <yourEntity> , "yourEntity".

, ODataController ( ODataQueryOptions ):

        public IEnumerable<Aggregate> GetOrders(ODataQueryOptions<ReportingOrder> opts, [FromUri(Name="$groupby")]string groupby, [FromUri(Name="$aggregates")]string aggregates = null)
        {
            var url = opts.Request.RequestUri.AbsoluteUri;


            int? top = null;

            if (opts.Top != null)
            {
                top = int.Parse(opts.Top.RawValue);

                var topStr = string.Format("$top={0}", top.Value);
                url = url.Replace(topStr, "");
                var req = new HttpRequestMessage(HttpMethod.Get, url);

                opts = new ODataQueryOptions<ReportingOrder>(opts.Context, req);

            }

            var query = opts.ApplyTo(db.ReportingOrders.AsQueryable()) as IQueryable<ReportingOrder>;

            var results = query.GroupBy(groupby, aggregates, top);

            return results;
        }
0

All Articles