I’m returning an ODataResult from the new Web API OData package as follows:
public ODataResult<Product> Get(ODataQueryOptions options)
{
var results = (options.ApplyTo(_db.Products) as IQueryable<Product>);
var count = results.Count;
var limitedResults = results.Take(100).ToArray();
return new ODataResult<Product>(results,null,count);
}
The above works great but it always returns an Atom response regardless of the query type. How can I return JSON?
I can see that Atom is the first supported media type within the ODataMediaTypeFormatter collection. I’d be happy just completely removing Atom support as I don’t need it. Even better would be to have the content type correctly negotiated.
Have you tried setting the accept header, like this:
Accept=application/json;odata=verbose.
That should return JSON.
The OData protocol has supported JSON for a while but in V3 of the protocol application/json is mapped to something called JSON light (which is not yet implemented). So to until JSON light is supported to get JSON you have to be a little more specific, and request the more verbose version of JSON that has been in OData since V1.