首页 > 解决方案 > 带有 JSON 数据的大型 POST 请求在 ASP.NET Core 4.6.1 中未反序列化

问题描述

我有一个网络 API:

[Route("api/MyResource")]
public IHttpActionResult Post([FromBody] MyDTO myResource, int resourceId)

其中 MyDTO 包含很多其他 DTO 数组。

当我发送较小的请求时,一切正常,但是在 MyDTO 的数组中有数千个对象(cca 5 MB)时,myResource 为空。

我也尝试将 myResource 参数的类型更改为object,但得到了相同的结果。较小的请求被反序列化为JObject,大的被反序列化为 null。我在输出窗口或错误 http 响应中没有异常。

我正在 IIS-Express 中调试应用程序。

为什么我得到 null 以及如何解决它?

标签: c#iisasp.net-web-apijson.net.net-4.6.1

解决方案


您可以更新maxAllowedContentLengthweb.config 中的

<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="52428800" />
  </requestFiltering>
</security>

如果不成功也设置

<system.web>
  <httpRuntime maxRequestLength="65536" />
</system.web>

推荐阅读