Search code examples
asp.net-web-apijson.netodataasp.net-web-api2

Queryable Web API 2 OData serialization


I have a lighweight setup where I am using Web API 2 with OData but do not create an "/odata" endpoint with its accompanying "/odata/$metadata" but instead just use standard ApiController's with attribute routing and Get methods marked with an override of [Queryable]: [InlineCountQueryable] detailed here Web API, OData, $inlinecount and testing.

It all works very well except that I can only expose dedicated model classes without any collections or object properties because the JSON serializer serializes the whole object graph. My ideal behavior would be to just have all collections ignored completely and object properties only included on $expand.

I believe this can be made to work because an OData Web Api 2 scaffold VS 2013 creates over an entity framework context has similar behavior, but I would like to keep this as lean as possible and avoid going that route as I found it to be a bit brittle and this service does not need to expose a $metadata description.

Update

I did some digging in the aspnet samples and found the "CustomODataFormatter" sample which seems to expose the machinery I would need to modify.. however the ODataSerializerProvider/ODataSerializer setup outlined there does not work with with attribute routing at all (any method defined with [Route("Some/Url")]).

Given that this seems to be what I need, does anyone know if attribute routing and the odata serializer provider mechanism should work together?


Solution

  • [JsonIgnore] or [DataMember] can be used to ignore properties for json.net serializer. webapi.odata 5.2 supports ODataRouteAttribute, an example is here.