我用ASP.NET CORE 2.1写了很长一段时间的工作项目,但是昨天,我被迫把它升级到.Net CORE 3.0(因为2.1已经不能调用用3.0写的DLL).

With that, a lot of functions were obsolete or already removed. I fixed almost all of it, but one problem with CORS.

像我之前的许多人一样,我用过:

app.UseCors(x => x
  .AllowAnyOrigin()
  .AllowAnyMethod()
  .AllowAnyHeader()
  .AllowCredentials());

in Configure function. And services.AddCors() in ConfigureServices function.

我可以很容易地通过设置WithOrigins().SetIsOriginAllowed(_ => true)来解决这个问题,而不是设置AllowAnyOrigin(),这在AllowCredentials()下不再起作用.

在那之后,我能够启动应用程序,我认为一切都很好,但后来我陷入了我不知道该如何解决的问题,直到现在.

我有DB关系N:N和处理它的关系表,这意味着我有Admin个实体和AdminProject list个属性,然后我有AdminProject个实体和Admin listProject list个属性,还有Project个实体和AdminProject list个属性.

当我列出某个管理员的项目时,我在控制器return Ok(projects)中返回,在控制器return Ok(projects)中,我只在AdminProject实体上使用getAll,然后使用Select只返回项目.

For that, I have to use[JsonIgnore] in project/admin for properties which I do not need to avoid cycling when creating json.

With that said: NOW IN .NET CORE 3.0 AND CORS SETTINGS IT DOES NOT WORK.

I am getting an error: System.Text.Json.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.

when debugging in console and error Access to XMLHttpRequest at 'http://localhost:5000/api/project/adminlist/1' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. in WEB browser

I think I tried almost everything with Cors settings etc and I do not know why is this happening now. I also tried to JsonConvert.SerializeObject() before return it ---> return Ok(JsonConvert.SerializeObject(projects)) and this is working, but I am not able (mentally) to do this in every single controllers functions.

请帮帮我!非常感谢!

推荐答案

The problem was occurring because in .NET Core 3 they change little bit the JSON politics. Json.Net is not longer supported and if you want to used all Json options, you have to download this Nuget: Microsoft.AspNetCore.Mvc.NewtonsoftJson.

之后,在Startup.cs file change/fix/add行中添加MVC(在ConfigureServices方法中).

因此:以下是我所做的以及解决我的问题的方法:

services.AddMvc(option => option.EnableEndpointRouting = false)
                .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                .AddNewtonsoftJson(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);

I hope it will help somebody else. Cheers!

Json相关问答推荐

替换字符串中特殊字符的Jolt变换

在Jenkins中使用ReadJSON读取json子元素

如何在Gatsby/Reaction中获取JSON-File子 node

将数组中的值作为键连接到另一个数组中的值(Jolt)

从Postgres表中的JSON中提取值

使用 jq 重新格式化 JSON 输出

Moshi:序列化 List 时出现问题

如何编写 jolt 规范以将不同的对象转换为数组

jq 中的整个单词匹配

Jolt 变换 - 如何用字段值重命名字段?

使用 System.Text.Json 序列化记录成员

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组

将 JSON 数据导入 Google 表格

apple-app-site-association json 文件是否会在应用程序中更新?

将字符串映射到json对象的多种类型?

在 Django 1.9 中,使用 JSONField(本机 postgres jsonb)的约定是什么?

使用 Retrofit 解析动态密钥 Json 字符串

JSON 模式 - 如果对象*不*包含特定属性则有效

将多个值存储在json中的单个键中

从 Ruby 中的 JSON 文件解析并从嵌套哈希中提取数字