【发布时间】:2015-10-20 16:22:18
【问题描述】:
在 Web API Controller 中的 action 方法返回 C# 类,像这样:
public class ShipController : ApiController
{
[HttpGet]
public Cell GetShip()
{
return new Cell(new ScriptEngine());
}
}
Cell 是我的类,继承自 Jurassic JS 库的 ObjectInstance。当我调用此操作时,ASP.NET 尝试将我的对象序列化为 XML 或 JSON,并且我得到 System.InvalidOperationException: "The 'ObjectContent`1' type failed to serialize response body for content type 'application/xml; charset= utf-8'。”我还尝试将 [DataContract] 属性添加到类,因为我发现 here,就像这样:
[DataContract]
public class Cell : ObjectInstance
{
[DataMember]
public int cellId;
public Cell(ScriptEngine engine) : base(engine)
{
}
}
但我仍然得到错误。如何让操作只返回我序列化的类字段,而不是进入父类?
【问题讨论】: