简体   繁体   English

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

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

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. 我在使用Visual Studio调试器服务器的本地计算机上工作正常。 But after I uploaded the code to the server (IIS 6). 但是我将代码上传到服务器(IIS 6)之后。 It gives a 500 Internal Server Error . 它给出了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". 在一个样本数据集中,我的json的长度是5743,我得到了“成功”。 But if the json size goes up to 17345, I got a 500 Internal Server Error . 但如果json大小达到17345,我得到500 Internal Server Error

So I tried to increase the limit of json. 所以我试图增加json的限制。 Base on this post 基于这篇文章

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? 请注意我使用的是IIS 6.所以这是一个问题吗? How can I correctly increase the limit of json size so that my data can hit the WebApi action? 如何正确增加json大小的限制,以便我的数据可以达到WebApi操作?

Thank you for any assistance. 谢谢你的帮助。

Try adding the aspnet:MaxJsonDeserializerMembers under appSettings in web.config 尝试在web.config appSettings下添加aspnet:MaxJsonDeserializerMembers

<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请求失败,但有异常

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

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