【发布时间】:2020-04-27 00:34:47
【问题描述】:
我知道这里有关于该特定问题的问题和答案,但我的问题有点独特(我猜)。
这是我的模型类:
public class RoleMaster
{
[Key]
[Required]
public int Id { get; set; }
[Required]
[StringLength(20)]
public string Role { get; set; }
}
这是我的控制器:
public class RolesController : ControllerBase
{
private readonly IRolesRepository _repo;
public RolesController(IRolesRepository repo)
{
_repo = repo;
}
[HttpGet]
public IActionResult Get()
{
var roles = _repo.GetRoles();
return new JsonResult(roles);
}
}
我的仓库:
public interface IRolesRepository
{
Task<IEnumerable<RoleMaster>> GetRoles();
}
这里是 GetRoles 方法:
public async Task<IEnumerable<RoleMaster>> GetRoles()
{
try
{
var roles = await db.RoleMaster.AsNoTracking().ToListAsync();
return roles;
}
catch (Exception)
{
throw;
}
}
这是我得到的错误:
处理请求时发生未处理的异常。 JsonException:检测到不支持的可能对象循环。这可能是由于循环或对象深度大于最大允许深度 32 造成的。 System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(int maxDepth)
从其他问题中我发现,如果您对其他表有引用,则可能会发生这种错误,但在我的情况下,没有涉及其他表。这是我创建的第一个表,正在尝试 get 方法。
【问题讨论】:
-
您能否发布
GetRoles()实际返回的内容,这会导致您的异常? -
mmm 那么我会说确实有问题..堆栈跟踪还有更多吗?
-
我认为你的问题在假设错误之后可能是 _repo.GetRoles();没有使用
await这个var roles = _repo.GetRoles();应该是var roles = await _repo.GetRoles();,你的混合异步和非异步,如果正确的错误帖子作为答案
标签: c# .net asp.net-core jsonexception