How can I increase the JSON deserialization MaxDepth limit in my ASP.NET Core webapp

我与影子孤独终老i 提交于 2020-08-07 07:49:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!