简体   繁体   English

WebAPI v2-OData的JSON序列化扩展导致$ type不正确

[英]WebAPI v2 - JSON Serialization for OData expand causes $type to be incorrect

I have just updated an MVC4/WebApi v1 application to MVC5 + WebAPI2.. 我刚刚将MVC4 / WebApi v1应用程序更新为MVC5 + WebAPI2。

Seems like after this upgrade the Json.Net serializer will no longer include the proper $type for requests that make use of OData $expand method. 似乎在此升级之后,对于使用OData $ expand方法的请求,Json.Net序列化程序将不再包括正确的$ type。 see below for an example of what i mean... 请参阅下面的示例说明我的意思...

CORRECT: 正确:

request: http://url.com/api/Studies/277/Sites 请求: http : //url.com/api/Studies/277/Sites

response: 响应:

{
$id: "1"
$type: "LGCYDAPI.Domain.Model.StudySiteWithContacts, LGCYDAPI.Domain.Model"
Contacts: [
{
$id: "2"
$type: "LGCYDAPI.Domain.Model.ContactRelatedToSite, LGCYDAPI.Domain.Model"
ContactID: -38445
}],
SiteID: 38445
}

INCORRECT : 不正确

request: http://url.com/api/Studies/277/Sites ?$expand=Contacts 请求: http : //url.com/api/Studies/277/Sites ?$ expand = Contacts

response: 响应:

{
$id: "1"
$type: "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"
Contacts: [
{
$id: "2"
$type: "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"
ContactID: -38445
}],
SiteID: 38445}

Along with MVC and WebAPI assemblies, I updated JSON.NET from 5.0.6 to 6.0.3 which I thought was the problem.. however, i reverted back to v5 and it didn't fix the problem so I'm back on v6. 连同MVC和WebAPI程序集,我将JSON.NET从5.0.6更新到了6.0.3,我认为这是问题所在。.但是,我又回到了v5,但它没有解决问题,所以我又回到了v6 。 Also updated OData nugets - my current versions are Microsoft.Data.OData=5.6.0 and Microsoft.AspNet.WebApi.OData=5.1.2. 还更新了OData nuget-我当前的版本是Microsoft.Data.OData = 5.6.0和Microsoft.AspNet.WebApi.OData = 5.1.2。

JSON Serializer Settings: JSON序列化器设置:

pConfiguration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
        pConfiguration.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
        pConfiguration.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;

        pConfiguration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

The $type in response payload is for the actual object to be serialized. 响应有效负载中的$ type用于要序列化的实际对象。

With $expand applied on the result, the structure of returned object is changed from LGCYDAPI.Domain.Model.StudySiteWithContacts to SelectExpandWrapper . 在结果上应用$ expand,返回对象的结构从LGCYDAPI.Domain.Model.StudySiteWithContacts更改为SelectExpandWrapper

So you can't get your expected Sites type with $expand applied. 因此,无法应用$ expand获得预期的站点类型。

For SelectExpandWrapper structure, check this: 对于SelectExpandWrapper结构,请检查以下内容:

https://aspnetwebstack.codeplex.com/wikipage?title=%24select%20and%20%24expand%20support https://aspnetwebstack.codeplex.com/wikipage?title=%24select%20and%20%24expand%20support

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM