WEB API JSON序列化循环引用 [英] WEB API JSON Serializing Circular References
本文介绍了WEB API JSON序列化循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Web API接收的子实体访问属性字段(JSON)。但是,通过查看浏览器控制台,它显示的是引用而不是字段。如何访问这些字段?
ANGULAR JS VIEW
< table infinite-scroll ='tF.loadMore()'infinite-scroll-disabled ='tF.isBusy'infinite-scroll-distance ='3'class =responsive>
< thead>
< tr>
< th> FIELD 1<
< th> FIELD 2<
< th> FIELD 3<
< th> FIELD 4<
<< th> FIELD 5<
< / tr>
< / thead>
< tbody>
< tr ng-repeat =tF.items中的项目| filter:searchFilter>
< td> {{item.CompanyDomainModel.CompanyName}}< / td>
< td> {{item.RatingDomainModel.RatingValue}}< / td>
< td> {{item.Views}}< / td>
< td> {{item.Clicks}}< / td>
< td> {{item.EmailSent}}< / td>
< / tr>
< / tbody>
< tfoot ng-show ='tF.isBusy'>
< tr>
< td colspan =9>< spinner show =tF.isBusy/>< span class =bold> {{tF.status}}< / span> < / TD>
< / tr>
< / tfoot>
< / table>
SERVICE
public ICollection< CompanyStatDomainModel> GetRecordsByPageSize(int page){
const int pgeSize = 20;
var result = _companyStatRepo
.AllIncluding(c => c.CompanyDomainModel,c => c.RatingDomainModel)
.OrderBy(c => c.CompanyStatId)
.Skip(page * pgeSize)
.Take(pgeSize)
.ToList();
返回结果;
}
ENDPOINT
IHttpActionResult GetRecordsByPageSize(int page){
var companyStatService = new CompanyStatService();
return Ok(companyStatService.GetRecordsByPageSize(page));
}
评分域模型
public class RatingDomainModel:IObjectWithState
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DataMember]
public int RatingId {get;组; }
[DataMember]
public int CompanyId {get;组; }
[DataMember]
public int UserId {get;组; }
[DataMember]
public int RatingValue {get;组; }
[DataMember]
public DateTime CreatedDate {get;组; }
// [ForeignKey(UserId)]
[DataMember]
public virtual UserDomainModel UserDomainModel {get;组; }
// [ForeignKey(CompanyId)]
[DataMember]
public virtual CompanyDomainModel CompanyDomainModel {get;组; }
[DataMember]
public virtual ICollection< CompanyStatDomainModel> CompanyStatDomainModels {get;组;
[NotMapped]
public Common.DataObject.State state {get;组; }
[NotMapped]
public bool InDb
{
get {return this.RatingId!= default(int); }
}
public object PersistenceEntityId
{
get {return this.RatingId;
}
}
OUTPUT p>
解决方案
将以下代码添加到WebApiConfig.cs中
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
I'm trying to access the property fields (JSON) from the child entity that is received from Web API. By looking at the browser console, however, it is showing a reference instead of the fields. How do I get access to these fields?
ANGULAR JS VIEW
<table infinite-scroll='tF.loadMore()' infinite-scroll-disabled='tF.isBusy' infinite-scroll-distance='3' class="responsive">
<thead>
<tr>
<th>FIELD 1</th>
<th>FIELD 2</th>
<th>FIELD 3</th>
<th>FIELD 4</th>
<th>FIELD 5</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in tF.items | filter:searchFilter">
<td>{{item.CompanyDomainModel.CompanyName}}</td>
<td>{{item.RatingDomainModel.RatingValue}}</td>
<td>{{item.Views}}</td>
<td>{{item.Clicks}}</td>
<td>{{item.EmailSent}}</td>
</tr>
</tbody>
<tfoot ng-show='tF.isBusy'>
<tr>
<td colspan="9"><spinner show="tF.isBusy" /><span class="bold">{{tF.status}}</span> </td>
</tr>
</tfoot>
</table>
SERVICE
public ICollection<CompanyStatDomainModel> GetRecordsByPageSize(int page) {
const int pgeSize = 20;
var result = _companyStatRepo
.AllIncluding(c => c.CompanyDomainModel, c => c.RatingDomainModel)
.OrderBy(c => c.CompanyStatId)
.Skip(page * pgeSize)
.Take(pgeSize)
.ToList();
return result;
}
ENDPOINT
IHttpActionResult GetRecordsByPageSize(int page) {
var companyStatService = new CompanyStatService();
return Ok(companyStatService.GetRecordsByPageSize(page));
}
RATING DOMAIN MODEL
public class RatingDomainModel : IObjectWithState
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DataMember]
public int RatingId { get; set; }
[DataMember]
public int CompanyId { get; set; }
[DataMember]
public int UserId { get; set; }
[DataMember]
public int RatingValue { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
//[ForeignKey("UserId")]
[DataMember]
public virtual UserDomainModel UserDomainModel { get; set; }
//[ForeignKey("CompanyId")]
[DataMember]
public virtual CompanyDomainModel CompanyDomainModel { get; set; }
[DataMember]
public virtual ICollection<CompanyStatDomainModel> CompanyStatDomainModels { get; set; }
[NotMapped]
public Common.DataObject.State state { get; set; }
[NotMapped]
public bool InDb
{
get { return this.RatingId != default(int); }
}
public object PersistenceEntityId
{
get { return this.RatingId; }
}
}
OUTPUT
解决方案
Added the below code into WebApiConfig.cs
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
这篇关于WEB API JSON序列化循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文