Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


.NET 5 HttpPost Model null if too large and other issues
Doing HTTPPost from angualarJs to .NET 5, discovered that if the model got too large (had large lists), it came across as null.

If controller was decorated with [ApiController], it returned error:
Failed to read the request form. Form value count limit 1024 exceeded
Otherwise, it was a silent cast to null.

Trying to exceed count in Startup.cs did not seem to work.
Had to decorate controller (actually decorated the base controller to apply to entire site) with the following: (choose values as needed)
[RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue, ValueCountLimit = int.MaxValue)]
[RequestSizeLimit(int.MaxValue)]

Other reasons can come across as null:
Param name is same spelling as a child property or an ignored query string (check build warnings for "1004' code)
Model doesn't match (use the following to make JSon serializer less strict):
Startup.cs - ConfigureServices:
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.DefaultBufferSize *= 10;
options.JsonSerializerOptions.MaxDepth = 100;
options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
})

Also fails if models contain cyclical references. If child classes point to a parent, decorate cyclical properties with "[JsonIgnore]".

Created By: amos 12/9/2021 11:14:08 AM
Updated: 12/9/2021 12:07:06 PM