I'm trying to use JSON.NET as a default serializer in WebAPI 2 stack. I've implemented JsonMediaTypeFormatter, in which I've used JSON.NET serializer for serialize/deserialize data and created JsonContentNegotiator for using this media type formatter. All works fine except OData querying - if I add [Queryable] metadata ot action method, then response object doesn't contains any metadata information, only list of entities.
Small example. My action method:
[Queryable]
public async Task<PageResult<RuleType>> GetRuleType(ODataQueryOptions<RuleType> options)
{
var ret = await _service.ListRuleTypesAsync(options);
return new PageResult<RuleType>(
ret,
Request.GetNextPageLink(),
Request.GetInlineCount());
}
If I use default OData serialize and call some query by Rule type (for example - .../odata/RuleType?$inlinecount=allpages&$skip=0&$top=1
), I receive classic OData response with metadata info and count property:
odata.metadata ".../odata/$metadata#RuleType"
odata.count "2"
value
0 {
Id: 1
Name: "General"
Code: "General"
Notes: null
}
(some fields skipped, but I have Notes property with null value)
But if I add my JsonContentNegotiator
with JsonMediaTypeFormatter
as a serializer - I receive only list of entities:
[
{
"Id": 1,
"Name": "General",
"Code": "General"
}
]
(no Notes field here because of NullValueHandling.Ignore
)
Even more. If I remove [Queryable]
attribute in action method - I receive another result:
{
"Items": [
{
"Id": 1,
"Name": "General",
"Code": "General"
}
],
"Count": 2
}
In this case I've received Count, but still no metadata here. And also odata response property names completely differs from default.
My mind is blowing up. I just want to use JSON.NET as my serializer in any part of my web app (because of some strong restrictions). How can I do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…