jQuery 如何增加 ASP.NET WebAPI Post 调用的 json 大小限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18260804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:17:02  来源:igfitidea点击:

How to increase the json size limit for ASP.NET WebAPI Post call?

jqueryjsonasp.net-web-apijson.net

提问by Blaise

My form on the page is a little too large. It collects the monthly measurement data. Please take a look at the sample:

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

{
"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}
}

So I am posting the big data to an APIController.

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

I works fine in my local machine using the Visual Studio debugger server. But after I uploaded the code to the server (IIS 6). It gives a 500 Internal Server Error.

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

I tried posting some sample data and checked the length of the stringified json.

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

In one sample data set, the length of my json is 5743, and I got a "success". But if the json size goes up to 17345, I got a 500 Internal Server Error.

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

So I tried to increase the limit of json. Base on this post

所以我试图增加json的限制。基于这篇文章

In Web.Config:

Web.Config

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

But it is not working.

但它不起作用。

There is another answer, using:

还有另一个答案,使用:

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

But where should we place this code? in Global.asax?

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

Please note I am using an IIS 6. So is it a problem? How can I correctly increase the limit of json size so that my data can hit the WebApi action?

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

Thank you for any assistance.

感谢您提供任何帮助。

回答by Andrés Nava - Azure

Try adding the aspnet:MaxJsonDeserializerMembersunder appSettingsin web.config

尝试添加aspnet:MaxJsonDeserializerMembersappSettingsweb.config

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

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

来源: 具有大量表单键、文件或 JSON 有效负载成员的 ASP.NET 请求失败并出现异常