为什么从 .net core 3.1 迁移到 .net 5 时 JSON return 值发生了变化
Why JSON return value has changed when migrating from .net core 3.1 to .net 5
我有一个 ASP.NET Web API,我已经从 .NET Core 3.1 迁移到 .NET 5,似乎我的 JSON 中的 return 值已经因此改变了。我环顾四周,但找不到任何关于为什么会发生这种情况的信息,我在下面包含了两个 JSON return 值。
我才发现这是一个问题,因为当我升级到 .NET 5 时,我的 Blazor API 调用抛出了一个 JSON 异常,表明它无法映射到现在有意义的模型我已经从 Postman 的每个版本中拉回 JSON。
任何帮助或信息将不胜感激。
C#代码
// GET: api/a/get-all
[HttpGet("get-all")]
public async Task<IActionResult> GetAll()
{
var as = await _context.A.Include(p => p.PT)
.Include(bt => bt.BT)
.Include(bf => bf.BF)
.Include(e => e.E)
.Include(b => b.B)
.Include(ar => ar.Ar)
.Include(a => a.A)
.Include(o => o.O)
.Include(c => c.C)
.Include(b => b.B)
.ToListAsync();
if (as == null)
return NotFound(new { errorMessage = "It seems there are no A's in the database, please enter at least one." });
return Ok(as);
}
.网络核心 3.1
[
{
"id": 1,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
},
{
"id": 4,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
}
]
.净5
{
"$id": "1",
"$values": [
{
"$id": "2",
"id": 1,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
},
{
"$id": "4",
"id": 4,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
}
]
}
我的 Blazor 项目在映射到模型时遇到了问题,因为反序列化器不需要 JSON 中的元数据。在我的 JsonSerializerOptions 中,我添加了 ReferenceHandler ReferenceHandler.Preserve 选项,这解决了我的问题。
JsonSerializerOptions options = new()
{
ReferenceHandler = ReferenceHandler.Preserve
};
我有一个 ASP.NET Web API,我已经从 .NET Core 3.1 迁移到 .NET 5,似乎我的 JSON 中的 return 值已经因此改变了。我环顾四周,但找不到任何关于为什么会发生这种情况的信息,我在下面包含了两个 JSON return 值。
我才发现这是一个问题,因为当我升级到 .NET 5 时,我的 Blazor API 调用抛出了一个 JSON 异常,表明它无法映射到现在有意义的模型我已经从 Postman 的每个版本中拉回 JSON。
任何帮助或信息将不胜感激。
C#代码
// GET: api/a/get-all
[HttpGet("get-all")]
public async Task<IActionResult> GetAll()
{
var as = await _context.A.Include(p => p.PT)
.Include(bt => bt.BT)
.Include(bf => bf.BF)
.Include(e => e.E)
.Include(b => b.B)
.Include(ar => ar.Ar)
.Include(a => a.A)
.Include(o => o.O)
.Include(c => c.C)
.Include(b => b.B)
.ToListAsync();
if (as == null)
return NotFound(new { errorMessage = "It seems there are no A's in the database, please enter at least one." });
return Ok(as);
}
.网络核心 3.1
[
{
"id": 1,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
},
{
"id": 4,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
}
]
.净5
{
"$id": "1",
"$values": [
{
"$id": "2",
"id": 1,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
},
{
"$id": "4",
"id": 4,
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
}
]
}
我的 Blazor 项目在映射到模型时遇到了问题,因为反序列化器不需要 JSON 中的元数据。在我的 JsonSerializerOptions 中,我添加了 ReferenceHandler ReferenceHandler.Preserve 选项,这解决了我的问题。
JsonSerializerOptions options = new()
{
ReferenceHandler = ReferenceHandler.Preserve
};