我正在使用jQuery的自动完成功能。当我尝试检索超过17000个记录的列表(每个记录的长度不超过10个)时,它超出了长度并抛出错误:
例外信息:
异常类型:InvalidOperationException
异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。
我可以在maxJsonLength
中为web.config
设置无限长度吗?如果没有,我可以设置的最大长度是多少?
答案 0 :(得分:677)
注意:此答案仅适用于Web服务,如果您从Controller方法返回JSON,请务必阅读下面的以下SO答案:https://stackoverflow.com/a/7207539/1246870
MaxJsonLength属性不能无限制,是一个默认为102400(100k)的整数属性。
您可以在web.config上设置MaxJsonLength属性:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
答案 1 :(得分:431)
如果您使用的是MVC 4 ,请务必查看 this answer 。
如果您仍然收到错误:
maxJsonLength
属性设置为web.config 你的问题很可能是:
MaxJsonLength属性的值仅适用于异步通信层用于调用Web服务方法的内部JavaScriptSerializer实例。 (MSDN: ScriptingJsonSerializationSection.MaxJsonLength Property)
基本上,“内部”JavaScriptSerializer
在从Web方法调用时会尊重maxJsonLength
的值;直接使用JavaScriptSerializer
(或通过MVC操作方法/控制器使用)不尊重maxJsonLength
属性,至少不是systemWebExtensions.scripting.webServices.jsonSerialization
部分web.config。
作为一种变通方法,您可以在Controller中(或任何地方)执行以下操作:
var serializer = new JavaScriptSerializer();
// For simplicity just use Int32's max value.
// You could always read the value from the config section mentioned above.
serializer.MaxJsonLength = Int32.MaxValue;
var resultData = new { Value = "foo", Text = "var" };
var result = new ContentResult{
Content = serializer.Serialize(resultData),
ContentType = "application/json"
};
return result;
这个答案是我对this asp.net forum answer的解释。
答案 2 :(得分:318)
在MVC 4中你可以这样做:
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult()
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
MaxJsonLength = Int32.MaxValue
};
}
在您的控制器中。
增加:
对于任何被您需要指定的参数困惑的人来说,调用可能如下所示:
Json(
new {
field1 = true,
field2 = "value"
},
"application/json",
Encoding.UTF8,
JsonRequestBehavior.AllowGet
);
答案 3 :(得分:58)
您可以在web.config文件中配置json请求的最大长度:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="....">
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
maxJsonLength的默认值为 102400 。有关详细信息,请参阅此MSDN页面:http://msdn.microsoft.com/en-us/library/bb763183.aspx
答案 4 :(得分:33)
我在ASP.NET Web窗体中遇到了这个问题。它完全忽略了web.config文件设置所以我这样做了:
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
return serializer.Serialize(response);
当然总的来说这是一种可怕的做法。如果要在Web服务调用中发送这么多数据,您应该看一下不同的方法。
答案 5 :(得分:27)
如果在web.config设置之后仍然出现错误,如下所示:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
&#13;
我通过以下方式解决了这个问题:
public ActionResult/JsonResult getData()
{
var jsonResult = Json(superlargedata, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
我希望这应该有所帮助。
答案 6 :(得分:21)
我修好了。
//your Json data here
string json_object="........";
JavaScriptSerializer jsJson = new JavaScriptSerializer();
jsJson.MaxJsonLength = 2147483644;
MyClass obj = jsJson.Deserialize<MyClass>(json_object);
效果很好。
答案 7 :(得分:16)
如果在您的web.config中实现上述添加后,您会收到“无法识别的配置部分system.web.extensions。”错误,请尝试将此添加到<ConfigSections>
部分中的web.config:< / p>
<sectionGroup name="system.web.extensions" type="System.Web.Extensions">
<sectionGroup name="scripting" type="System.Web.Extensions">
<sectionGroup name="webServices" type="System.Web.Extensions">
<section name="jsonSerialization" type="System.Web.Extensions"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
答案 8 :(得分:14)
我遵循了vestigal的回答并得到了这个解决方案:
当我需要将大型json发布到控制器中的动作时,我会在使用JSON JavaScriptSerializer进行反序列化时获得着名的&#34;错误。字符串的长度超过maxJsonLength属性上设置的值。\ r \ nParameter name:input value provider&#34;。
我所做的是创建一个新的ValueProviderFactory,LargeJsonValueProviderFactory,并在GetDeserializedObject方法中设置MaxJsonLength = Int32.MaxValue
public sealed class LargeJsonValueProviderFactory : ValueProviderFactory
{
private static void AddToBackingStore(LargeJsonValueProviderFactory.EntryLimitedDictionary backingStore, string prefix, object value)
{
IDictionary<string, object> dictionary = value as IDictionary<string, object>;
if (dictionary != null)
{
foreach (KeyValuePair<string, object> keyValuePair in (IEnumerable<KeyValuePair<string, object>>) dictionary)
LargeJsonValueProviderFactory.AddToBackingStore(backingStore, LargeJsonValueProviderFactory.MakePropertyKey(prefix, keyValuePair.Key), keyValuePair.Value);
}
else
{
IList list = value as IList;
if (list != null)
{
for (int index = 0; index < list.Count; ++index)
LargeJsonValueProviderFactory.AddToBackingStore(backingStore, LargeJsonValueProviderFactory.MakeArrayKey(prefix, index), list[index]);
}
else
backingStore.Add(prefix, value);
}
}
private static object GetDeserializedObject(ControllerContext controllerContext)
{
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return (object) null;
string end = new StreamReader(controllerContext.HttpContext.Request.InputStream).ReadToEnd();
if (string.IsNullOrEmpty(end))
return (object) null;
var serializer = new JavaScriptSerializer {MaxJsonLength = Int32.MaxValue};
return serializer.DeserializeObject(end);
}
/// <summary>Returns a JSON value-provider object for the specified controller context.</summary>
/// <returns>A JSON value-provider object for the specified controller context.</returns>
/// <param name="controllerContext">The controller context.</param>
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
object deserializedObject = LargeJsonValueProviderFactory.GetDeserializedObject(controllerContext);
if (deserializedObject == null)
return (IValueProvider) null;
Dictionary<string, object> dictionary = new Dictionary<string, object>((IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase);
LargeJsonValueProviderFactory.AddToBackingStore(new LargeJsonValueProviderFactory.EntryLimitedDictionary((IDictionary<string, object>) dictionary), string.Empty, deserializedObject);
return (IValueProvider) new DictionaryValueProvider<object>((IDictionary<string, object>) dictionary, CultureInfo.CurrentCulture);
}
private static string MakeArrayKey(string prefix, int index)
{
return prefix + "[" + index.ToString((IFormatProvider) CultureInfo.InvariantCulture) + "]";
}
private static string MakePropertyKey(string prefix, string propertyName)
{
if (!string.IsNullOrEmpty(prefix))
return prefix + "." + propertyName;
return propertyName;
}
private class EntryLimitedDictionary
{
private static int _maximumDepth = LargeJsonValueProviderFactory.EntryLimitedDictionary.GetMaximumDepth();
private readonly IDictionary<string, object> _innerDictionary;
private int _itemCount;
public EntryLimitedDictionary(IDictionary<string, object> innerDictionary)
{
this._innerDictionary = innerDictionary;
}
public void Add(string key, object value)
{
if (++this._itemCount > LargeJsonValueProviderFactory.EntryLimitedDictionary._maximumDepth)
throw new InvalidOperationException("JsonValueProviderFactory_RequestTooLarge");
this._innerDictionary.Add(key, value);
}
private static int GetMaximumDepth()
{
NameValueCollection appSettings = ConfigurationManager.AppSettings;
if (appSettings != null)
{
string[] values = appSettings.GetValues("aspnet:MaxJsonDeserializerMembers");
int result;
if (values != null && values.Length > 0 && int.TryParse(values[0], out result))
return result;
}
return 1000;
}
}
}
然后,在Global.asax.cs的Application_Start方法中,将ValueProviderFactory替换为新的:
protected void Application_Start()
{
...
//Add LargeJsonValueProviderFactory
ValueProviderFactory jsonFactory = null;
foreach (var factory in ValueProviderFactories.Factories)
{
if (factory.GetType().FullName == "System.Web.Mvc.JsonValueProviderFactory")
{
jsonFactory = factory;
break;
}
}
if (jsonFactory != null)
{
ValueProviderFactories.Factories.Remove(jsonFactory);
}
var largeJsonValueProviderFactory = new LargeJsonValueProviderFactory();
ValueProviderFactories.Factories.Add(largeJsonValueProviderFactory);
}
答案 9 :(得分:10)
您可以将此行写入Controller
json.MaxJsonLength = 2147483644;
您也可以将此行写入web.config
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647">
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
`
为了安全起见,请同时使用它们。
答案 10 :(得分:9)
如果从MVC中的MiniProfiler收到此错误,则可以通过将属性MiniProfiler.Settings.MaxJsonResponseSize
设置为所需值来增加该值。默认情况下,此工具似乎忽略config中设置的值。
MiniProfiler.Settings.MaxJsonResponseSize = 104857600;
答案 11 :(得分:7)
只需在MVC的Action方法中设置MaxJsonLength proprty
JsonResult json= Json(classObject, JsonRequestBehavior.AllowGet);
json.MaxJsonLength = int.MaxValue;
return json;
答案 12 :(得分:6)
我建议将其设置为Int32.MaxValue。
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
答案 13 :(得分:6)
某些属性魔法怎么样?
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MaxJsonSizeAttribute : ActionFilterAttribute
{
// Default: 10 MB worth of one byte chars
private int maxLength = 10 * 1024 * 1024;
public int MaxLength
{
set
{
if (value < 0) throw new ArgumentOutOfRangeException("value", "Value must be at least 0.");
maxLength = value;
}
get { return maxLength; }
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
JsonResult json = filterContext.Result as JsonResult;
if (json != null)
{
if (maxLength == 0)
{
json.MaxJsonLength = int.MaxValue;
}
else
{
json.MaxJsonLength = maxLength;
}
}
}
}
然后,您可以使用全局过滤器配置或控制器/操作方式全局应用它。
答案 14 :(得分:4)
问题是你真的需要返回17k记录吗?您打算如何处理浏览器中的所有数据?无论如何,用户不会滚动浏览17000行。
更好的方法是只检索“前几个”记录并根据需要加载更多。
答案 15 :(得分:3)
对于那些在MVC3中遇到问题的人,JSON会自动为模型绑定器反序列化并且太大,这是一个解决方案。
感谢http://blog.naver.com/techshare/100145191355和https://gist.github.com/DalSoft/1588818让我指出了如何做到这一点的正确方向。第一个站点上的最后一个链接包含解决方案的完整源代码。
答案 16 :(得分:3)
您可以像其他人所说的那样在配置中设置它,或者您可以设置序列化程序的单个实例,如:
var js = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
答案 17 :(得分:3)
JsonResult result = Json(r);
result.MaxJsonLength = Int32.MaxValue;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
答案 18 :(得分:2)
我解决了添加此代码的问题:
String confString = HttpContext.Current.Request.ApplicationPath.ToString();
Configuration conf = WebConfigurationManager.OpenWebConfiguration(confString);
ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)conf.GetSection("system.web.extensions/scripting/webServices/jsonSerialization");
section.MaxJsonLength = 6553600;
conf.Save();
答案 19 :(得分:2)
刚碰到这个。我收到了超过6,000条记录。刚刚决定我做一些分页。在,我接受我的MVC JsonResult端点中的页码,默认为0,所以没有必要,如下所示:
public JsonResult MyObjects(int pageNumber = 0)
然后而不是说:
return Json(_repository.MyObjects.ToList(), JsonRequestBehavior.AllowGet);
我说:
return Json(_repository.MyObjects.OrderBy(obj => obj.ID).Skip(1000 * pageNumber).Take(1000).ToList(), JsonRequestBehavior.AllowGet);
这非常简单。然后,在JavaScript中,而不是:
function myAJAXCallback(items) {
// Do stuff here
}
我改为说:
var pageNumber = 0;
function myAJAXCallback(items) {
if(items.length == 1000)
// Call same endpoint but add this to the end: '?pageNumber=' + ++pageNumber
}
// Do stuff here
}
并将您的记录附加到您最初使用它们的任何内容中。或者等到所有通话完成并将结果拼凑在一起。
答案 20 :(得分:2)
似乎没有“无限”价值。默认值为2097152个字符,相当于4 MB的Unicode字符串数据。
正如已经观察到的,在浏览器中很难使用17,000条记录。如果要呈现聚合视图,则在服务器上执行聚合并在浏览器中仅传输摘要可能更有效。例如,考虑一个文件系统浏览器,我们只看到树的顶部,然后在我们向下钻取时发出更多请求。每个请求中返回的记录数量相对较少。树视图演示文稿可以很好地适用于大型结果集。
答案 21 :(得分:2)
如果您在View中遇到此类问题,可以使用以下方法解决此问题。这里使用了 Newtonsoft 包。
@using Newtonsoft.Json
<script type="text/javascript">
var partData = @Html.Raw(JsonConvert.SerializeObject(ViewBag.Part));
</script>
答案 22 :(得分:1)
替代ASP.NET MVC 5修复:
(我的MFC类似于上面的回答,只做了一些小改动)
我还没准备好改用Json.NET,在我的情况下,错误发生在请求期间。我的方案中的最佳方法是修改将修复程序应用于全局项目的实际JsonValueProviderFactory
,并且可以通过编辑global.cs
文件来完成。
JsonValueProviderConfig.Config(ValueProviderFactories.Factories);
添加web.config条目:
<add key="aspnet:MaxJsonLength" value="20971520" />
然后创建以下两个类
public class JsonValueProviderConfig
{
public static void Config(ValueProviderFactoryCollection factories)
{
var jsonProviderFactory = factories.OfType<JsonValueProviderFactory>().Single();
factories.Remove(jsonProviderFactory);
factories.Add(new CustomJsonValueProviderFactory());
}
}
这基本上是System.Web.Mvc
中的默认实现的精确副本,但添加了可配置的web.config appsetting value aspnet:MaxJsonLength
。
public class CustomJsonValueProviderFactory : ValueProviderFactory
{
/// <summary>Returns a JSON value-provider object for the specified controller context.</summary>
/// <returns>A JSON value-provider object for the specified controller context.</returns>
/// <param name="controllerContext">The controller context.</param>
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
object deserializedObject = CustomJsonValueProviderFactory.GetDeserializedObject(controllerContext);
if (deserializedObject == null)
return null;
Dictionary<string, object> strs = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
CustomJsonValueProviderFactory.AddToBackingStore(new CustomJsonValueProviderFactory.EntryLimitedDictionary(strs), string.Empty, deserializedObject);
return new DictionaryValueProvider<object>(strs, CultureInfo.CurrentCulture);
}
private static object GetDeserializedObject(ControllerContext controllerContext)
{
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
string fullStreamString = (new StreamReader(controllerContext.HttpContext.Request.InputStream)).ReadToEnd();
if (string.IsNullOrEmpty(fullStreamString))
return null;
var serializer = new JavaScriptSerializer()
{
MaxJsonLength = CustomJsonValueProviderFactory.GetMaxJsonLength()
};
return serializer.DeserializeObject(fullStreamString);
}
private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, object value)
{
IDictionary<string, object> strs = value as IDictionary<string, object>;
if (strs != null)
{
foreach (KeyValuePair<string, object> keyValuePair in strs)
CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakePropertyKey(prefix, keyValuePair.Key), keyValuePair.Value);
return;
}
IList lists = value as IList;
if (lists == null)
{
backingStore.Add(prefix, value);
return;
}
for (int i = 0; i < lists.Count; i++)
{
CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakeArrayKey(prefix, i), lists[i]);
}
}
private class EntryLimitedDictionary
{
private static int _maximumDepth;
private readonly IDictionary<string, object> _innerDictionary;
private int _itemCount;
static EntryLimitedDictionary()
{
_maximumDepth = CustomJsonValueProviderFactory.GetMaximumDepth();
}
public EntryLimitedDictionary(IDictionary<string, object> innerDictionary)
{
this._innerDictionary = innerDictionary;
}
public void Add(string key, object value)
{
int num = this._itemCount + 1;
this._itemCount = num;
if (num > _maximumDepth)
{
throw new InvalidOperationException("The length of the string exceeds the value set on the maxJsonLength property.");
}
this._innerDictionary.Add(key, value);
}
}
private static string MakeArrayKey(string prefix, int index)
{
return string.Concat(prefix, "[", index.ToString(CultureInfo.InvariantCulture), "]");
}
private static string MakePropertyKey(string prefix, string propertyName)
{
if (string.IsNullOrEmpty(prefix))
{
return propertyName;
}
return string.Concat(prefix, ".", propertyName);
}
private static int GetMaximumDepth()
{
int num;
NameValueCollection appSettings = ConfigurationManager.AppSettings;
if (appSettings != null)
{
string[] values = appSettings.GetValues("aspnet:MaxJsonDeserializerMembers");
if (values != null && values.Length != 0 && int.TryParse(values[0], out num))
{
return num;
}
}
return 1000;
}
private static int GetMaxJsonLength()
{
int num;
NameValueCollection appSettings = ConfigurationManager.AppSettings;
if (appSettings != null)
{
string[] values = appSettings.GetValues("aspnet:MaxJsonLength");
if (values != null && values.Length != 0 && int.TryParse(values[0], out num))
{
return num;
}
}
return 1000;
}
}
答案 23 :(得分:0)
如果您只想对引起问题的特定操作进行修复,那么您的代码如下:
public JsonResult GetBigJson()
{
var someBigObject = GetBigObject();
return Json(someBigObject);
}
您可以将其更改为以下变体:
public JsonResult GetBigJson()
{
var someBigObject = GetBigObject();
return new JsonResult()
{
Data = someBigObject,
JsonRequestBehavior = JsonRequestBehavior.DenyGet,
MaxJsonLength = int.MaxValue
};
}
功能应该相同,您可以返回更大的JSON作为响应。
您可以检查Controller.Json
方法在ASP.NET MVC source code中的作用
protected internal JsonResult Json(object data)
{
return Json(data, null /* contentType */, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
}
它正在调用其他Controller.Json
方法:
protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior
};
}
传递的contentType
和contentEncoding
对象是null
。因此,基本上在控制器中调用return Json(object)
等效于调用return new JsonResult { Data = object, JsonRequestBehavior = sonRequestBehavior.DenyGet }
。您可以使用第二种形式并参数化JsonResult
。
那么当您设置MaxJsonLength
属性(默认情况下为空)时会发生什么?
它是passed down到JavaScriptSerializer.MaxJsonLength
属性,然后JavaScriptSerializer.Serialize
方法是called:
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
serializer.MaxJsonLength = MaxJsonLength.Value;
}
if (RecursionLimit.HasValue)
{
serializer.RecursionLimit = RecursionLimit.Value;
}
response.Write(serializer.Serialize(Data));
如果您未设置序列化器的MaxJsonLenght
属性,则将takes default value设为2MB。
答案 24 :(得分:0)
我们不需要任何服务器端更改。 您可以修复此问题,只能通过web.config文件进行修改 这对我有帮助。试试这个
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
<add key="aspnet:UpdatePanelMaxScriptLength" value="2147483647" />
</appSettings>
and
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647"/>
</webServices>
</scripting>
答案 25 :(得分:0)
WebForms UpdatePanel解决方案:
向Web.config添加设置:
<configuration>
<appSettings>
<add key="aspnet:UpdatePanelMaxScriptLength" value="2147483647" />
</appSettings>
</configuration>
https://support.microsoft.com/en-us/kb/981884
ScriptRegistrationManager
类包含以下代码:
// Serialize the attributes to JSON and write them out
JavaScriptSerializer serializer = new JavaScriptSerializer();
// Dev10# 877767 - Allow configurable UpdatePanel script block length
// The default is JavaScriptSerializer.DefaultMaxJsonLength
if (AppSettings.UpdatePanelMaxScriptLength > 0) {
serializer.MaxJsonLength = AppSettings.UpdatePanelMaxScriptLength;
}
string attrText = serializer.Serialize(attrs);
答案 26 :(得分:0)
使用lib\Newtonsoft.Json.dll
public string serializeObj(dynamic json) {
return JsonConvert.SerializeObject(json);
}
答案 27 :(得分:-2)
您不需要使用web.config 您可以在通过列表的catch值期间使用short属性 例如 声明像
这样的模型public class BookModel
{
public decimal id { get; set; } // 1
public string BN { get; set; } // 2 Book Name
public string BC { get; set; } // 3 Bar Code Number
public string BE { get; set; } // 4 Edition Name
public string BAL { get; set; } // 5 Academic Level
public string BCAT { get; set; } // 6 Category
}
这里我用的是比较短的比例 BC =条形码 BE =书籍版等
答案 28 :(得分:-2)
如果这个maxJsonLength值是一个int那么它的int 32bit / 64bit / 16bit有多大....我只想确定我可以设置的最大值是什么maxJsonLength
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647">
</jsonSerialization>
</webServices>
</scripting>