简体   繁体   中英

Web API POST parameter is null for large JSON request

I have a POST method in Web API controller that takes a class with 50 fields as parameter. I am getting the parameter value as null in the controller, but if I reduce the number of fields to 30 or so, I am getting the right 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 am getting the object with right values.

Is this the preferred way of reading a POST parameter?

Set the httpRuntime value under system.web section in web.config

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

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

There were 4 things we had to do for our .NET Web Api project (.NET Framework):

1.In the web.config add:

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

2. Also add to the web.config:

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

3. Large requests need 64 bitness: In Web Api project properties, when running locally in IIS Express, set the bitness to 64: 在此输入图像描述 When published, make sure the app pool is supporting 64-bit.

4. We noticed the requests hogging up memory for an extended period of time: Have your api controllers implement a base api controller. In that base api controller override the dispose method and dispose of the garbage:

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

        GC.Collect();
    }

I do not recommend forcing garbage collection. You should use visual studios built in diagnostics to take snap shots before and after the problems, then compare the memory to see what is eating it up.

please create properties for all your parameters and pass to post method as class object. ex.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM