Web API POST parameter is null for a large JSON request

I have a POST method in a Web API controller that takes a class with 50 fields as a parameter. I get the parameter value as null in the controller, but if I reduce the number of fields to 30 or so, I get the correct value.

I have this added to Web.Config:

Add key = "aspnet: MaxJsonDeserializerMembers" value = "140000"

If I use Request.Content.ReadAsStreamAsync() and use the JsonSerializer to deserialize the stream, I get an object with the correct values.

Is this the preferred way to read the POST parameter?

+7
source share
3 answers

Set httpRuntime to system.web in web.config

 <httpRuntime maxRequestLength="50000"></httpRuntime> 

The maximum request size in kilobytes. The default size is 4096 KB (4 MB).

+12
source

We needed to do 4 things for our .NET Web Api project (.NET Framework):

1. In web.config add:

 <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="4294967295" /> </requestFiltering> </security> 

2. Also add to web.config:

 <system.web> <httpRuntime targetFramework="4.7.1" maxRequestLength="2147483647" /> 

3. For large queries, 64-bit is required. In the properties of the Web Api project, when running locally in IIS Express, set 64-bit: enter image description here When publishing, ensure that the application pool supports the 64-bit version.

4. We noticed that requests take up memory for a long period of time: let your API controllers implement the base API controller. In this database, the api controller overrides the dispose method and removes the garbage:

 protected override void Dispose(bool disposing) { base.Dispose(disposing); GC.Collect(); } 

I do not recommend forcing garbage collection. You should use the visual studios built into the diagnostics to take pictures before and after the problems, and then compare the memory to see what it absorbs.

0
source

create properties for all your parameters and go to the post method as an object of the class. ex.

 public class clsProperty{ public param1 {get;set;} public param2 {get;set;} } 

[HttpPost] public void postmethod ([frombody] clsProperty)

-4
source

Source: https://habr.com/ru/post/1261679/


All Articles