I'm struggling to add OData v4 query support to a method on a controller that inherits from ApiController rather than ODataController. While I have a working OData model in the solution there are some endpoints that don't really belong in the model but the power of the query would be useful.
I've seen some articles suggesting I can just return an 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; }
}
All I get back when a call /Test/Events is a 406 Not Acceptable, something that I've run into a lot dealing with OData that usually means I've return something the OData framework doesn't like. I've never been able to get anymore info out of the framework (a major failing I think) as to why it doesn't like it and only solved them by trial and error in the past.
Has anyone got this setup working and if so how?
Or any suggestions on debugging the 406 response?
EDIT:
OK so it turned out the culprit was so code in the start up that was registering a custom ODataMediaTypeFormatter and in the process was clearing all other formatters.
After removing the offending code it worked.
Really really wish WebApi would record somewhere what caused the 406 error when it spits one out.