本文介绍了.NET CORE 3 升级 CORS 和 Json(cycle) XMLHttpRequest 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作项目用asp.net core 2.1写了很长时间,但昨天我被迫升级到.net core 3.0(由于2.1无法调用已经用3.0编写的Dll).

I had my working project written in asp.net core 2.1 for a long time, but yesterday, I was forced to upgrade it to .net core 3.0 (due to 2.1 cannot call Dll' s which are written in 3.0 already).

因此,许多功能已过时或已删除.我修复了几乎所有问题,但 CORS 存在一个问题.

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

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

Like many people before me, I used:

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

Configure 函数中.以及 ConfigureServices 函数中的 services.AddCors().

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

我可以通过设置 WithOrigins().SetIsOriginAllowed(_ => true) 而不是 AllowAnyOrigin() 轻松解决这个问题code> 不再与 AllowCredentials() 一起使用.

I was able to fixed this quite easily with setting WithOrigins() or .SetIsOriginAllowed(_ => true) instead of AllowAnyOrigin() which does not work anymore with AllowCredentials().

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

After that, I was able to start the application and I thought everything is fine, but then I get stuck until now with problem I do not know, how to fix.

我有数据库关系 N:N 和处理它的关系表,这意味着我有 Admin 实体和 AdminProject list 属性,然后我有 AdminProject 具有 Admin listProject list 属性的实体以及具有 AdminProject list 属性的 Project 实体.

I have DB relation N:N and relation table which handle that, that means I have Admin entity with AdminProject list property, then I have AdminProject entity with Admin list and Project list properties and Project entity with AdminProject list property once again.

当我列出某个管理员的项目时,我在 Controller 中返回这个 return Ok(projects),我只在 AdminProjectgetAll/code> 实体,然后用 Select 返回仅项目.

When I am listing my projects of certain admin, I am returning in Controller this return Ok(projects), where I just use getAll on AdminProject entity and then with Select return only project.

为此,我必须在项目/管理中使用[JsonIgnore] 来获取我在创建 json 时不需要避免循环的属性.

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

话虽如此:现在在 .NET CORE 3.0 和 CORS 设置中它不起作用.

我收到一个错误:System.Text.Json.JsonException:检测到不支持的可能的对象循环.这可能是由于循环或对象深度大于最大允许深度 32.

在控制台中调试和错误时 Access to XMLHttpRequest at 'http://localhost:5000/api/project/adminlist/1' from origin 'http://localhost:8080' has beenblocked by CORS policy: 请求的资源上不存在Access-Control-Allow-Origin"标头. 在 WEB 浏览器中

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

我想我尝试了几乎所有的 Cors 设置等,但我不知道为什么现在会发生这种情况.我还尝试在返回之前使用 JsonConvert.SerializeObject() ---> return Ok(JsonConvert.SerializeObject(projects)) 并且这是有效的,但我无法(精神上)在每个控制器都起作用.

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.

请帮忙!非常感谢!

推荐答案

出现问题是因为在 .NET Core 3 中,它们对 JSON 策略进行了一点点改动.不再支持 Json.Net,如果您想使用所有 Json 选项,您必须下载此 Nuget:Microsoft.AspNetCore.Mvc.NewtonsoftJson.

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 文件更改/修复/添加行中添加 MVC(在 ConfigureServices 方法中.

After that in your Startup.cs file change/fix/add line where you are adding MVC (in the ConfigureServices method.

所以:这是我所做的以及解决了我的问题的方法:

So: here is what I did and what fixed my issue:

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!

这篇关于.NET CORE 3 升级 CORS 和 Json(cycle) XMLHttpRequest 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:16
查看更多