Search code examples
odataasp.net-web-api2

custom OData v4 function always returns 406


So I'm trying to map this method as an Odata function ...

[HttpGet]
[EnableQuery]
public IHttpActionResult ForBuyerOrganisations([FromUri]int[] orgIds)
{
    // this returns IQueryable<TRDetails>
    // i tried throwing a ToList on there too to ensure it works and it does
    var result = service.ForBuyerOrgs(orgIds); 
    return Ok(result);
}

I have mapped it like this ...

var forBuyers = Builder.EntityType<TRDetails>().Collection.Function("ForBuyerOrganisations");
forBuyers.ReturnsCollection<TRDetails>();
forBuyers.CollectionParameter<int>("orgIds");

... I also tried this ...

var forBuyers = Builder.EntityType<TRDetails>().Collection.Function("ForBuyerOrganisations");
forBuyers.ReturnsCollectionFromEntitySet<TRDetails>();
forBuyers.CollectionParameter<int>("orgIds");

I can call it with the url:

~/TRDetails/ForBuyerOrganisations?orgIds=1

The Problem:

The request executes my code and returns from this method, then the client gets a 406.

Any Ideas?


Solution

  • Yes. @Ouyang indicates the config error. Besides, as I seem, there are other four errors:

    1. you should use [FromODataUri], not [FromUri] for the parameter.
    2. you should call the function with namespace-qualified.

      TRDetails/Namespace.ForBuyerOrganisations(...)

    3. you should set the argument as list, because you config it as collection.

    4. you should call the function using the () for parameter.

    So, the following request is sample request:

    ~/TRDetails/Default.ForBuyerOrganisations(orgIds=[5,4,2,3,1])