我用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 list
和Project 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.
请帮帮我!非常感谢!