I got a very simple controller that reads json from a local file, parses it into a JObject with JObjects.parse, and then returns it with Ok().
The result shows the JSON, however the values are "empty"
The test JSON i’m using
{
"version": "1.3",
"status": "ok"
}
If I add a breakpoint, the response variable seems to be correctly filled:
Breakpoint
Does anyone have any clue what could be the issue here?
>Solution :
ASP.NET Core for several versions (since 3rd version if I remember correctly) now uses System.Text.Json
as default serializer, which knows nothing about JObject
from Newtonsoft one. You need to either switch to Newtonsoft Json (see here for example) – add Microsoft.AspNetCore.Mvc.NewtonsoftJson
nuget and use AddNewtonsoftJson
(for example services.AddControllers().AddNewtonsoftJson();
)
Or use System.Text.Json
capabilities for example JsonNode
API (available since .NET 6).