总是有误差"该ObjectContent 1类型没有序列化响应主体..." [英] Always have error "The ObjectContent 1 type failed to serialize the response body..."
问题描述
我使用的Web API从数据库中检索数据。我只有1台tblMessage,想从该表中获取数据。
我把一切都弄好但后来当我运行的网站。错误总是说
在'ObjectContent`1类型没有序列化内容类型响应体应用程序/ XML
块引用>我读了一些计算器帖子说赛义德的错误可能是由告诉浏览器输出数据以JSON格式是固定的。此后,误差变得
在'ObjectContent`1类型没有序列化内容类型响应体应用/ JSON
块引用>我已经尝试了所有的解决方案从以下职位,但他们不解决问题(浏览器报告了同样的错误)
<一个href=\"http://social.msdn.microsoft.com/Forums/vstudio/en-US/a5adf07b-e622-4a12-872d-40c753417645/web-api-error-the-objectcontent1-type-failed-to-serialize-the-response-body-for-content\"相对=nofollow>网络API错误:ObjectContent`1类型没有序列化内容类型响应体
<一个href=\"http://stackoverflow.com/questions/14053109/failed-to-serialize-the-response-body-for-content-type\">Failed序列化响应正文内容类型
<一个href=\"http://stackoverflow.com/questions/12936713/web-api-error-the-objectcontent1-type-failed-to-serialize-the-response-body\">Web API错误:ObjectContent`1类型没有序列化内容类型响应主体
究竟这个错误是什么?
公共接口即时聊天
{
IQueryable的&LT;消息&GT;得到所有();
}公共类信息
{
[键]
公众诠释i_StmID {搞定;组; }
公共字符串vch_MsgString {搞定;组; }
}公共类EFDBContext:的DbContext
{
公共DbSet&LT;消息&GT;消息{搞定;组; } 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
base.OnModelCreating(模型构建器);
modelBuilder.Entity&LT;消息&GT;()ToTable(tblMessage);
}
}公共类MessageRepository:即时聊天
{
私人EFDBContext语境=新EFDBContext(); 公众的IQueryable&LT;消息&GT;得到所有()
{
返回context.tblMessage;
}
}公共类MessageController:ApiController
{
公共即时聊天回购=新MessageRepository(); 公共IEnumerable的&LT;消息&GT; GetAllMsg()
{
返回repo.GetAll();
}
}
解决方案修改
的IEnumerable&LT;消息
>为列表&LT;消息&GT;
公开的IEnumerable&LT;消息&GT; GetAllMsg()
{
返回repo.GetAll();
}到
公开名单&LT;消息&GT; GetAllMsg()
{
返回repo.GetAll();
}更新:
获得在本地内存OutOfMemoryException异常
,因为这种方法会存储所有消息
的对象,但要注意,所以你必须实现某种分页。I use Web api to retrieve data from the database. I only have 1 table "tblMessage" and want to get data from that table.
I set everything up but then when I run the website. the error always say
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml
I read some posts on stackoverflow that sayid the error could be fixed by telling the browser to output data in json format. After that, the error becomes
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json
I have tried all solutions from the following posts, but they dont fix the problem ( browser reports the same error)
Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type
Failed to serialize the response body for content type
Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type
What exactly this error is?
public interface IMessage { IQueryable<Message> GetAll(); } public class Message { [Key] public int i_StmID { get; set; } public string vch_MsgString { get; set; } } public class EFDBContext : DbContext { public DbSet<Message> Message { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Message>().ToTable("tblMessage"); } } public class MessageRepository : IMessage { private EFDBContext context = new EFDBContext(); public IQueryable<Message> GetAll() { return context.tblMessage; } } public class MessageController : ApiController { public IMessage repo = new MessageRepository(); public IEnumerable<Message> GetAllMsg() { return repo.GetAll(); } }
解决方案Change
IEnumerable<Message
> toList<Message>
public IEnumerable<Message> GetAllMsg() { return repo.GetAll(); }
to
public List<Message> GetAllMsg() { return repo.GetAll(); }
UPDATE: But beware of getting
OutOfMemoryException
because this method will store allMessage
objects in local memory so you have to implement some kind of paging.这篇关于总是有误差&QUOT;该ObjectContent 1类型没有序列化响应主体...&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!