Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid a 406 when receiving an OData.PageResult<T>?

I have an ODataController that is returning a PageResult.

Api Example:

public PageResult<Customer> Get(ODataQueryOptions options) {
// cut some stuff out...

    PageResult<Customer> result = new PageResult<Customer>(
        searchResults as IEnumerable<Customer>,
        Request.GetNextPageLink(),
        Request.GetInlineCount());
    return result;

When I debug this, it seems to be fine and have a PageResult class built up correctly to return. On the Web side..

Web Example

using (var client = new HttpClient()) {
    client.BaseAddress = new Uri(testURL);
    string searchUrl = "api/customer?$top=1&$skip=0";
    client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata=verbose"));
    HttpResponseMessage response = client.GetAsync(searchUrl).Result;

The response is a StatusCode 406, with a reason phrase stating the content was not acceptable. It also does this if I define a new MediaTypeWithQualityHeaderValue("application/json").

What do I need to change so that I successfully consume this Api in the controller before passing it on to the view?

like image 502
Eric98118 Avatar asked Oct 04 '22 14:10

Eric98118


1 Answers

I think you are missing the first two steps of building an OData service. ODataController, as the name says, only works with OData routes. You need to build an EDM model representing your OData service, and, add an OData route exposing that EDM model. Refer to this official documentation and blog post for details on how to build OData services.

like image 129
RaghuRam Nadiminti Avatar answered Oct 13 '22 10:10

RaghuRam Nadiminti