I know there are question and answers with that particular problem here on so, but my problem is little unique (I guess).
Here is my model class:
public class RoleMaster
{
[Key]
[Required]
public int Id { get; set; }
[Required]
[StringLength(20)]
public string Role { get; set; }
}
Here is my controller:
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);
}
}
My repository:
public interface IRolesRepository
{
Task<IEnumerable<RoleMaster>> GetRoles();
}
and here is the GetRoles method:
public async Task<IEnumerable<RoleMaster>> GetRoles()
{
try
{
var roles = await db.RoleMaster.AsNoTracking().ToListAsync();
return roles;
}
catch (Exception)
{
throw;
}
}
and this is the error I am getting:
An unhandled exception occurred while processing the request. JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(int maxDepth)
From other questions I found out that if you have references with other tables then this kind of error can occur but in my case there is no other table involved. This is the first table I have created and was just trying the get method.