在我的 web api 中,当我运行项目以从数据库获取数据时出现此错误 .net core 3.1
JsonException: 检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于最大允许深度 32。
这些是我的代码我的模型
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string ProductText { get; set; }
public int ProductCategoryId { get; set; }
[JsonIgnore]
public virtual ProductCategory ProductCategory { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的 productCategory 类是:
public class ProductCategory
{
public int Id { get; set; }
public string Name { get; set; }
public string CatText { get; set; …Run Code Online (Sandbox Code Playgroud) 我的工作项目用asp.net core 2.1编写了很长时间,但是昨天,我被迫将其升级到.net core 3.0(由于2.1无法调用已经用3.0编写的Dll)。
因此,许多功能已过时或已被删除。我几乎解决了所有问题,但CORS出现了一个问题。
像我之前的许多人一样,我曾经:
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
Run Code Online (Sandbox Code Playgroud)
在Configure功能上。并services.AddCors()在ConfigureServices功能上。
我能与设置固定这很容易WithOrigins()或.SetIsOriginAllowed(_ => true)代替AllowAnyOrigin()不与工作了AllowCredentials()。
在那之后,我能够启动该应用程序,并且我认为一切都很好,但是直到现在为止,我一直陷在一个我不知道如何解决的问题上。
我有数据库关系N:N和关系表来处理该问题,这意味着我具有Admin具有AdminProject list属性的实体,然后又具有AdminProject具有Admin list和Project list属性的Project实体以及具有AdminProject list属性的实体。
当我列出某些管理员的项目时,我将在Controller this中返回return Ok(projects),我只getAll在AdminProject实体上使用,然后Select仅返回项目。
为此,我必须[JsonIgnore]在project / admin中使用创建json时不需要避免循环的属性。
这样说:现在,.NET CORE 3.0和CORS设置不起作用了。
我收到一个错误:
System.Text.Json.JsonException: A possible object cycle was …
您对我们如何 使用System.Text.Json.JsonSerializer序列化 DataSet、DataTable有什么建议吗?
当前它抛出此异常:'检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于最大允许深度 64。
我知道这里有关于那个特定问题的问答,但我的问题并不独特(我猜)。
这是我的模型类:
public class RoleMaster
{
[Key]
[Required]
public int Id { get; set; }
[Required]
[StringLength(20)]
public string Role { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的存储库:
public interface IRolesRepository
{
Task<IEnumerable<RoleMaster>> GetRoles();
}
Run Code Online (Sandbox Code Playgroud)
这是 GetRoles 方法:
public async Task<IEnumerable<RoleMaster>> GetRoles()
{
try
{
var roles = await …Run Code Online (Sandbox Code Playgroud)