- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 Web api 从数据库中检索数据。我只有 1 个表“tblMessage”并想从该表中获取数据。
我设置了所有内容,但是当我运行网站时。错误总是说
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml
我在 stackoverflow 上读了一些帖子,说可以通过告诉浏览器以 json 格式输出数据来修复错误。之后,错误就变成了
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json
我已经尝试了以下帖子中的所有解决方案,但它们没有解决问题(浏览器报告相同的错误)
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
这个错误到底是什么?
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();
}
}
最佳答案
更改 IEnumerable<Message
> 至 List<Message>
public IEnumerable<Message> GetAllMsg()
{
return repo.GetAll();
}
到
public List<Message> GetAllMsg()
{
return repo.GetAll();
}
更新:但要小心得到 OutOfMemoryException
因为此方法将存储所有 Message
本地内存中的对象,因此您必须实现某种分页。
关于c# - 总是有错误 "The ObjectContent 1 type failed to serialize the response body...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134271/
我有以下要序列化的类: public class UpdateDoorCommand : IXmlSerializable { // string such as D1 public
我正在使用 System.Net.Http 的 HttpClient 通过“POST”调用 REST API使用以下代码: using (HttpRequestMessage requestMessa
var cnt = new ObjectContent(1, new JsonMediaTypeFormatter()); 上面的行在下面给出了编译器错误 'System.Net.Http.Objec
我似乎无法捕获从 System.Net.Http.HttpClient 的 SendAsync 方法抛出的某些异常。具体来说,是从 HttpWebRequest.ChechProtocol 方法抛出的
我有 2 个 POCO 类——Contact 和 TrainingSeminar——它们使用 EF5 加入多对多关系 在联系人 odata Controller 中,我希望能够返回联系人注册的 Tra
我使用 Web api 从数据库中检索数据。我只有 1 个表“tblMessage”并想从该表中获取数据。 我设置了所有内容,但是当我运行网站时。错误总是说 The 'ObjectContent`1'
这个简直要杀了我。这里的文章和网络都没有帮助。 首先,我正在使用 .Net 4.5 开发 ASP.Net WebForms(不是 MVC)。我找到了great article它可以帮助您将 OData
当我使用 ObjectContent 对象创建 HttpContent 以通过 HttpClient 向 Web API 服务发送请求时,出现以下错误: 不能向缓冲区写入超过配置的最大缓冲区大小的字节
对于我的单元测试,我想将 HttpResponseMessage 的内容转换为正确的类型,这样我就可以访问它的 Value 属性,如下所示: var content = result.Content
我在尝试使用 Web API Controller 时收到此错误。 Web API Error: The 'ObjectContent`1' type failed to serialize the
我更新到 MVC4 的最终版本,现在我的测试失败并出现此错误: System.MissingMethodException : Method not found: 'Void System.Net.H
最简单的示例,我获取一个集合并尝试通过 Web API 输出它: // GET api/items public IEnumerable Get() { return MyContext.It
我是一名优秀的程序员,十分优秀!