简体   繁体   English

对于大型JSON请求,Web API POST参数为null

[英]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. 我在Web API控制器中有一个POST方法,它接受一个包含50个字段的类作为参数。 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. 我在控制器中获取参数值为null ,但如果我将字段数减少到30左右,我得到的值正确。

I have this added to Web.Config: 我将此添加到Web.Config:

add key="aspnet:MaxJsonDeserializerMembers" value="140000" 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. 如果我使用Request.Content.ReadAsStreamAsync() ,并使用JsonSerializer反序列化流,我得到的对象具有正确的值。

Is this the preferred way of reading a POST parameter? 这是读取POST参数的首选方式吗?

Set the httpRuntime value under system.web section in web.config 在web.config中的system.web部分下设置httpRuntime

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

The maximum request size in kilobytes. 最大请求大小(以KB为单位)。 The default size is 4096 KB (4 MB). 默认大小为4096 KB(4 MB)。

There were 4 things we had to do for our .NET Web Api project (.NET Framework): 我们必须为.NET Web Api项目(.NET Framework)做4件事:

1.In the web.config add: 1.在web.config中添加:

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

2. Also add to the web.config: 2.还要添加到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: 3.大请求需要64位:在Web Api项目属性中,在IIS Express中本地运行时,将位数设置为64: 在此输入图像描述 When published, make sure the app pool is supporting 64-bit. 发布时,请确保应用程序池支持64位。

4. We noticed the requests hogging up memory for an extended period of time: Have your api controllers implement a base api controller. 4.我们注意到长时间占用内存的请求:让api控制器实现基本api控制器。 In that base api controller override the dispose method and dispose of the garbage: 在那个基础api控制器覆盖dispose方法并处理垃圾:

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. 请为所有参数创建属性,并将post方法作为类对象传递给post方法。 ex. 恩。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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