asp.net-mvc 获取“JSON 请求太大无法反序列化”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10966328/
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
Getting "The JSON request was too large to be deserialized"
提问by Kayvan Karim
I'm getting this Error:
我收到此错误:
The JSON request was too large to be deserialized.
JSON 请求太大,无法反序列化。
Here's a scenario where this occurs. I have a class of country which hold a list of shipping ports of that country
这是发生这种情况的场景。我有一类国家,其中包含该国家/地区的航运港口列表
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
public List<Port> Ports { get; set; }
}
I use KnockoutJS on the client side to make a cascading drop downs. So we have an array of two drop downs, where the first one is country, and the second one is ports of that country.
我在客户端使用 KnockoutJS 进行级联下拉。所以我们有一个包含两个下拉列表的数组,其中第一个是国家/地区,第二个是该国家/地区的港口。
Everything is working fine so far, this my client side script:
到目前为止一切正常,这是我的客户端脚本:
var k1 = k1 || {};
$(document).ready(function () {
k1.MarketInfoItem = function (removeable) {
var self = this;
self.CountryOfLoadingId = ko.observable();
self.PortOfLoadingId = ko.observable();
self.CountryOfDestinationId = ko.observable();
self.PortOfDestinationId = ko.observable();
};
k1.viewModel = function () {
var marketInfoItems = ko.observableArray([]),
countries = ko.observableArray([]),
saveMarketInfo = function () {
var jsonData = ko.toJSON(marketInfoItems);
$.ajax({
url: 'SaveMarketInfos',
type: "POST",
data: jsonData,
datatype: "json",
contentType: "application/json charset=utf-8",
success: function (data) {
if (data) {
window.location.href = "Fin";
} else {
alert("Can not save your market information now!");
}
},
error: function (data) { alert("Can not save your contacts now!"); }
});
},
loadData = function () {
$.getJSON('../api/ListService/GetCountriesWithPorts', function (data) {
countries(data);
});
};
return {
MarketInfoItems: marketInfoItems,
Countries: countries,
LoadData: loadData,
SaveMarketInfo: saveMarketInfo,
};
} ();
The problem occurs when a country like China is selected, which has lotsof ports. So if you have 3 or 4 times "China" in your array and I want to send it to the server to save. The error occurs.
The problem occurs when a country like China is selected, which has lotsof ports. 因此,如果您的阵列中有 3 或 4 个“ CN ”,并且我想将其发送到服务器进行保存。发生错误。
What should I do to remedy this?
我应该怎么做才能解决这个问题?
回答by VJAI
You have to adjust the maxJsonLengthproperty to a higher value in web.configto resolve the issue.
您必须将maxJsonLength属性调整为更高的值web.config才能解决此问题。
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
Set a higher value for aspnet:MaxJsonDeserializerMembersin the appSettings:
aspnet:MaxJsonDeserializerMembers在 appSettings 中设置更高的值:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
If those options are not working you could try creating a custom json value provider factory using JSON.NET as specified in this thread.
如果这些选项不起作用,您可以尝试使用此线程中指定的 JSON.NET 创建自定义 json 值提供程序工厂。
回答by gburton
If you don't want to change a global setting in the web config
如果您不想更改 Web 配置中的全局设置
Using a global setting will activate large json responses throughout your entire application which might open you up to a denial of service attack.
使用全局设置将在整个应用程序中激活大型 json 响应,这可能会使您面临拒绝服务攻击。
If a few choice locations are allowed this, you can very quickly use another json serialiser using the Content method like so:
如果允许选择几个位置,您可以使用 Content 方法非常快速地使用另一个 json 序列化器,如下所示:
using Newtonsoft.Json;
// ...
public ActionResult BigOldJsonResponse()
{
var response = ServiceWhichProducesLargeObject();
return Content(JsonConvert.SerializeObject(response));
}
// ...

