【发布时间】:2018-04-12 21:55:11
【问题描述】:
我有 web api 控制器,我尝试做这样的事情:
public IHttpActionResult Get()
{
var Rooms = db.Rooms.Select(r => new {
Id = r.Id,
Name = r.Name,
ChairNum = r.СhairNum,
Requests = r.Requests.ToList()
}).ToList();
return Ok(new { results = Rooms });
}
在模型中我有这个:
public partial class Room
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Room()
{
this.Requests = new HashSet<Request>();
}
public int Id { get; set; }
public string Name { get; set; }
public int СhairNum { get; set; }
public bool IsProjector { get; set; }
public bool IsBoard { get; set; }
public string Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Request> Requests { get; set; }
}
我试图在服务中传递这些数据。但是当我调用http://localhost:99999/api/Rest时出现序列化错误。
<ExceptionMessage>
The "ObjectContent`1" type could not serialize the response text for the content type "application / json; charset = utf-8".
</ ExceptionMessage>
将模型中的数据传递到 json 中的最佳方法是什么?
【问题讨论】:
-
序列化错误是什么意思?什么错误?它来自哪一行代码?
-
如果我从示例中删除 Requests = r.Requests.ToList() 则没有错误
-
你没有回复我的评论。
-
这里的答案可能会有所帮助:stackoverflow.com/questions/13959048/…
标签: asp.net json ajax asp.net-web-api