In .NET Core 3.0+ include the NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson
and then replace
services.AddControllers();
in ConfigureServices
with
services.AddControllers().AddNewtonsoftJson();
This is a pre-release NuGet package in .NET Core 3.0 but a full release package in .NET Core 3.1.
I came across this myself, but I've found that the same answer with some additional info is in this SO question and answer.
Edit: As a useful update: code with the call to AddNewtonsoftJson()
will compile and run even without installing the Microsoft.AspNetCore.Mvc.NewtonsoftJson
NuGet package. If you do that, it runs with both converters installed, but defaulting to the System.Text.Json
converter which you presumably don't want since you're reading this answer. So you must remember to install the NuGet package for this to work properly (and remember to re-install it if you ever clear down and redo your NuGet dependencies).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…