网页API错误:“ObjectContent`1”类型没有序列化内容类型响应体 [英] Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type

查看:1181
本文介绍了网页API错误:“ObjectContent`1”类型没有序列化内容类型响应体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Web API控制器时,收到此错误。

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

网页API错误:ObjectContent`1类型没有序列化内容类型响应体

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

在我的控制器中的code是如下:

the code in my controller is as follows

public IEnumerable<Student> GetAllStudents()
    {
        var allstudents = unitOfWork.StudentRepository.Get(includeProperties: "Groups");


        return allstudents;
    }

    public Student GetStudentByID(Guid id)
    {
        return unitOfWork.StudentRepository.GetByID(id);
    }

和我的'学生'类如下:

and my 'Student' class is as follows

public partial class Student
{
    public Student()
    {
        this.Groups = new HashSet<Group>();
    }

    public System.Guid StudentID { get; set; }
    public string Surname { get; set; }
    public string FirstName { get; set; }
    public byte[] Timestamp { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<Group> Groups { get; set; }
}

这两种方法都导致相同的错误。

Both methods result in the same error.

我内心的例外是如下:

类型
  'System.Data.Entity.DynamicProxies.Student_4C97D068E1AD0BA62C3C6E441601FFB7418AD2D635F7F1C14B64F4B2BE32DF9A'
  数据合同名称
  Student_4C97D068E1AD0BA62C3C6E441601FFB7418AD2D635F7F1C14B64F4B2BE32DF9A: http://schemas.datacontract.org/2004/07/ System.Data.Entity.DynamicProxies
  预计不会。考虑使用DataContractResolver或添加任何
  类型不是静态已知的已知类型的列表 - 例如,
  通过使用KnownTypeAttribute属性或通过将其添加到
  已知类型的列表传递给DataContractSerializer的

Type 'System.Data.Entity.DynamicProxies.Student_4C97D068E1AD0BA62C3C6E441601FFB7418AD2D635F7F1C14B64F4B2BE32DF9A' with data contract name 'Student_4C97D068E1AD0BA62C3C6E441601FFB7418AD2D635F7F1C14B64F4B2BE32DF9A:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

我有一种感觉,我需要使用 KnownType 属性,但我不完全知道如何实现它。

I have a feeling I need to use the KnownType attribute but I'm not exactly sure how to implement it.

任何帮助将AP preciated

Any help would be appreciated

推荐答案

什么是内部异常的消息?内部异常讯息会由该串行抛出的实际的异常和它应该告诉我们它类型是造成异常。

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.

让我猜猜 - 这是任何类型的课程和键入?如果是的话,可以尝试在 KnownType 属性的实际执行何种类型的类学生

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 {...}

这篇关于网页API错误:“ObjectContent`1”类型没有序列化内容类型响应体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆