Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type

后端 未结 2 1717
谎友^
谎友^ 2021-01-03 07:10

I am getting this error when attempting to use a Web API controller.

Web API Error: The \'ObjectContent`1\' type failed to serialize the response body

相关标签:
2条回答
  • 2021-01-03 07:21

    If you don't need the lazy-loaded navigation properties provided by the proxy class (System.Data.Entity.DynamicProxies.Student_4C97D068E1A...), you can disable their generation by setting:

    unitOfWork.Configuration.ProxyCreationEnabled = false;
    

    What to do if you need the proxy class is another question.


    Follow these links for a good overview of lazy loading and proxies:

    • Loading Related Entities
    • Working with Proxies
    • Should I enable or disable dynamic proxies

    I usually disable lazy loading and proxies by default, and enable one or both in specific code blocks that need them.

    0 讨论(0)
  • 2021-01-03 07:26

    What is the inner exception message? The inner exception message will be the actual exception that is thrown by the serializer and it should tell us which type is causing the exception.

    Let me guess -- Is it any the type Course and the type Group? If so, try putting KnownType attribute on the actual implementation type of your class Student

    [KnownType(typeof(GroupA))]
    [KnownType(typeof(CourseA))]
    public partial class Student
    {...}
    
    public class GroupA : Group {...}
    public class CourseA : Course {...}
    
    public interface Group {...}
    public interface Course {...}
    
    0 讨论(0)
提交回复
热议问题