问题
We're using ASP.NET Core 2.1 with .NET Framework 4.6.2.
We have a customer who needs to send up a rather largely nested json structure to our webapp.
When they make this call, we're outputting the following log and returning an error:
The reader's MaxDepth of 32 has been exceeded. Path 'super.long.path.to property', line 1, position 42111."
I've looked through the ASP.NET Core codebase, and have observed a couple of references to MaxDepth = 32
in the deserializer provided with the framework.
Is it possible to configure this to a different depth?
回答1:
I have not tested this, but in Startup.cs you should be able to do something like this:
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.MaxDepth = 64; // or however deep you need
});
回答2:
You could also set depth you need directly in deserialization as shown in the following example:
JsonConvert.DeserializeObject< T >(json, new JsonSerializerSettings
{
MaxDepth = //the value you need
});
回答3:
if you are using .net core 3.1 then try this code in your startup.cs file. Make sure you install Newtonsoft.Json package
services.AddMvc().AddNewtonsoftJson(options =>
{
options.SerializerSettings.MaxDepth = 64;
});
来源:https://stackoverflow.com/questions/58138212/how-can-i-increase-the-json-deserialization-maxdepth-limit-in-my-asp-net-core-we