Asp.Net Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'

前端 未结 8 1617
天命终不由人
天命终不由人 2020-11-27 16:56

Simplest example of this, I get a collection and try to output it via Web API:

// GET api/items
public IEnumerable Get()
{
    return MyContext.I         


        
相关标签:
8条回答
  • 2020-11-27 17:41

    If you have navigation properties make them non virtual. Mapping will still work but it prevents the creation of Dynamic Proxy entities which cannot be serialized.]

    Not having lazy loading is fine in a WebApi as you don't have a persistent connection and you ran a .ToList() anyway.

    0 讨论(0)
  • 2020-11-27 17:45

    In my case the object being returned had a property within it with a type that did not have an argumentless/default constructor. By adding a zero-argument constructor to that type the object could be serialized successfully.

    0 讨论(0)
提交回复
热议问题