C# ';ObjectContent`1';类型未能序列化内容类型';的响应正文;应用程序/json;字符集=utf-8';
几个月来,以下web API 2(简化示例)正确响应C# ';ObjectContent`1';类型未能序列化内容类型';的响应正文;应用程序/json;字符集=utf-8';,c#,json.net,C#,Json.net,几个月来,以下web API 2(简化示例)正确响应 http://localhost/TestApi2/api/tests with this. [{"Id":1,"Name":"Naples"},{"Id":2,"Name":"Paris"}] //using Newtonsoft.Json; namespace TestApi2.Controllers { public class PersonsController : ApiControlle
http://localhost/TestApi2/api/tests with this.
[{"Id":1,"Name":"Naples"},{"Id":2,"Name":"Paris"}]
//using Newtonsoft.Json;
namespace TestApi2.Controllers
{
public class PersonsController : ApiController
{
[HttpGet]
[Route("api/tests")]
public HttpResponseMessage GetAll()
{
// This is a dbContext generated from a DatabaseFirst EF Generation
using (var cleanDbContext = new cleanDbEntities())
{
var tests = cleanDbContext.TestTables;
// Return all
return Request.CreateResponse(HttpStatusCode.OK, tests, Configuration.Formatters.JsonFormatter);
}
}
}
}
var tests = cleanDbContext.TestTables.ToList();
然后我更新了我在这个项目上的NUGET
同一个电话的回应是:
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
var tests = cleanDbContext.TestTables.ToList();
修改查询以显式生成列表修复了该问题。我会将此信息发送到Newtonsoft。这是一款很棒的产品。您确定没有更新EF软件包吗?可能的重复项您确定没有更新EF软件包吗?可能的重复项
var tests = cleanDbContext.TestTables.ToList();