.NETMVCJSONJavaScriptSerializer字符串的长度超过
maxJso。。。
1 [ArgumentException: 使⽤ JSON JavaScriptSerializer 序列化或还原序列化期间发⽣错误。字符串的长度超过在 maxJsonLength 属性上设定的值。
2参数名称: input]
3    System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +168
4    System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +213
5    System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +16
6    System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +69
7    System.Web._ValueProvider() +30 
由于前端 Post 到 Action 的参数太⼤,超过了2M,还没进⼊后台的 Action ⽅法就报错了。这个问题困扰了很久,⼀直未解决。⽹上了⼏个⽅法都⽆效。
在 fig 中加⼊这些,没有作⽤:
1 2 3 4<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" /> <add key="aspnet:UpdatePanelMaxScriptLength" value="2147483647" /> </appSettings>
在 fig 中加⼊这些,也没有作⽤:
1 2 3 4 5 6 7 8<sions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647"> </jsonSerialization>
</webServices>
</scripting>
</sions>
仔细看了⼀下异常信息,发现,是在
System.Web.Mvc.JsonValueProviderFactory ⾥调⽤的 JavaScriptSerializer:于是查了⼀下 ,发现 JsonValueProviderFactory 在 System.Web.Mvc.dll 程序集⾥的:
反编译 System.Web.Mvc.dll 到 JsonValueProviderFactory 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Web.Mvc.Properties;
using System.Web.Script.Serialization;
namespace System.Web.Mvc
{
public sealed class JsonValueProviderFactory : ValueProviderFactory
{
private class EntryLimitedDictionary
{
private static int_maximumDepth = JsonValueProviderFactory.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 > JsonValueProviderFactory.EntryLimitedDictionary._maximumDepth)
{
throw new InvalidOperationException(MvcResources.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;
}
}
return1000;
}
}
private static void AddToBackingStore(JsonValueProviderFactory.EntryLimitedDictionary backingStore, string prefix, object value) {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
IDictionary<string, object> dictionary = value as IDictionary<string, object>;
if(dictionary != null)
{
foreach(KeyValuePair<string, object> current in dictionary)
{
JsonValueProviderFactory.AddToBackingStore(backingStore, JsonValueProviderFactory.MakePropertyKey(prefix, current.Key), current.Value);
}
return;
}
IList list = value as IList;
if(list != null)
{
for(int i = 0; i < list.Count; i++)
{
JsonValueProviderFactory.AddToBackingStore(backingStore, JsonValueProviderFactory.MakeArrayKey(prefix, i), list[i]);
}
return;
}
backingStore.Add(prefix, value);
}
private static object GetDeserializedObject(ControllerContext controllerContext)
{
if(!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
{
return null;
}
StreamReader streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
string text = streamReader.ReadToEnd();
if(string.IsNullOrEmpty(text))mvc的controller
{
return null;
}
// 问题就出在这⾥,没有给 javaScriptSerializer.MaxJsonLength 赋值,其默认值是 2097152 字节,即2M
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
return javaScriptSerializer.DeserializeObject(text);
}
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if(controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
object deserializedObject = JsonValueProviderFactory.GetDeserializedObject(controllerContext);
if(deserializedObject == null)
{
return null;
}
Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
JsonValueProviderFactory.EntryLimitedDictionary backingStore = new JsonValueProviderFactory.EntryLimitedDictionary(dictionary);
JsonValueProviderFactory.AddToBackingStore(backingStore, string.Empty, deserializedObject);
return new DictionaryValueProvider<object>(dictionary, CultureInfo.CurrentCulture);
}
private static string MakeArrayKey(string prefix, int index)
{
return prefix + "["+ index.ToString(CultureInfo.InvariantCulture) + "]";
}
private static string MakePropertyKey(string prefix, string propertyName)
{
if(!string.IsNullOrEmpty(prefix))
{
return prefix + "."+ propertyName;
}
return propertyName;
}
113}
在 JavaScriptSerializer 没有设置 MaxJsonLength,默认值是 2097152 字节,即2M。 
解决此问题的⽅法就是 把 javaScriptSerializer.MaxJsonLength = int.MaxValue; (int.MaxValue 值是 2147483647 字节,即2048M)
⾃⼰重写类 JsonValueProviderFactory 命名为 MyJsonValueProviderFactory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Web.Mvc;
using System.Web.Mvc.Properties;
using System.Web.Script.Serialization;
namespace XXX
{
public sealed class MyJsonValueProviderFactory : ValueProviderFactory
{
private class EntryLimitedDictionary
{
private static int_maximumDepth = 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 > _maximumDepth)
{
//throw new InvalidOperationException(MvcResources.JsonValueProviderFactory_RequestTooLarge);
throw new InvalidOperationException("itemCount is over maximumDepth");
}
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;
}
}
return1000;
}
}
private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, object value)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
IDictionary<string, object> dictionary = value as IDictionary<string, object>;
if(dictionary != null)
{
foreach(KeyValuePair<string, object> current in dictionary)
{
AddToBackingStore(backingStore, MakePropertyKey(prefix, current.Key), current.Value);
}
return;
}
IList list = value as IList;
if(list != null)
{
for(int i = 0; i < list.Count; i++)
{
AddToBackingStore(backingStore, MakeArrayKey(prefix, i), list[i]);
}
return;
}
backingStore.Add(prefix, value);
}
private static object GetDeserializedObject(ControllerContext controllerContext)
{
if(!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) {
return null;
}
StreamReader streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
string text = streamReader.ReadToEnd();
if(string.IsNullOrEmpty(text))
{
return null;
}
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
// 解决这个问题:
// 使⽤ JSON JavaScriptSerializer 序列化或还原序列化期间发⽣错误。字符串的长度超过在 maxJsonLength 属性上设定的值。javaScriptSerializer.MaxJsonLength = int.MaxValue;
// ----------------------------------------
return javaScriptSerializer.DeserializeObject(text);
}
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if(controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
object deserializedObject = GetDeserializedObject(controllerContext);
if(deserializedObject == null)
{
return null;
}
Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); EntryLimitedDictionary backingStore = new EntryLimitedDictionary(dictionary);
AddToBackingStore(backingStore, string.Empty, deserializedObject);
return new DictionaryValueProvider<object>(dictionary, CultureInfo.CurrentCulture);
}
private static string MakeArrayKey(string prefix, int index)
{
return prefix + "["+ index.ToString(CultureInfo.InvariantCulture) + "]";
}
private static string MakePropertyKey(string prefix, string propertyName)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。