【问题标题】:How to increase the json size limit for ASP.NET WebAPI Post call?如何增加 ASP.NET WebAPI Post 调用的 json 大小限制?
【发布时间】:2023-03-17 20:40:01
【问题描述】:

我在页面上的表格有点太大了。它收集每月的测量数据。请看示例:

{
"Year":2013,
"Month":3,
"Records":[
    {"Id":0,"Date":"3/1/2013","RiverSection":5,"UserNumber":56},
    {"Id":0,"Date":"3/1/2013","RiverSection":7,"UserNumber":200},
    {"Id":0,"Date":"3/1/2013","RiverSection":8,"UserNumber":556},
    {"Id":0,"Date":"3/2/2013","RiverSection":5,"UserNumber":56},
    {"Id":0,"Date":"3/2/2013","RiverSection":7,"UserNumber":200},
    ...
    ...
    {"Id":0,"Date":"3/31/2013","RiverSection":7,"UserNumber":200}
}

所以我将大数据发布到 APIController。

我使用 Visual Studio 调试器服务器在本地机器上工作正常。但是在我将代码上传到服务器(IIS 6)之后。它给出了一个500 Internal Server Error。

我尝试发布一些示例数据并检查了字符串化 json 的长度。

在一个示例数据集中,我的 json 长度为 5743,我得到了“成功”。但是如果 json 大小增加到 17345,我得到一个500 Internal Server Error。

所以我尝试增加json的限制。基于this post

在Web.Config:

<system.web.extensions>
<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="5000000">
    </jsonSerialization>
  </webServices>
</scripting>
</system.web.extensions>

但它不起作用。

还有另一个答案,使用:

var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;

但是我们应该把这段代码放在哪里呢?在Global.asax?

请注意,我使用的是 IIS 6。这有问题吗?如何正确增加 json 大小的限制,以便我的数据可以命中 WebApi 操作?

感谢您的帮助。

【问题讨论】:

标签: jquery json asp.net-web-api json.net


【解决方案1】:

尝试在web.config 中的appSettings 下添加aspnet:MaxJsonDeserializerMembers

<add key="aspnet:MaxJsonDeserializerMembers" value="20000"/>

来源: An ASP.NET request that has lots of form keys, files, or JSON payload members fails with an exception

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2023-02-15
    • 2019-02-27
    • 1970-01-01
    • 2012-04-26
    • 2016-10-20
    • 1970-01-01
    相关资源
    最近更新 更多