Pra*_*sad 648 asp.net asp.net-mvc json
我正在使用jQuery的自动完成功能.当我尝试检索超过17000条记录的列表(每条记录的长度不超过10条)时,它超出了长度并抛出错误:
异常信息:
异常类型:InvalidOperationException
异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过maxJsonLength属性上设置的值.
我可以设置无限长度maxJsonLength的web.config?如果没有,我可以设置的最大长度是多少?
CMS*_*CMS 705
注意:此答案仅适用于Web服务,如果您从Controller方法返回JSON,请确保您也阅读以下SO答案:https://stackoverflow.com/a/7207539/1246870
所述MaxJsonLength属性不能是无限的,是整数属性默认为102400(10万).
您可以在web.config上设置MaxJsonLength属性:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Dav*_*och 448
如果您使用的是MVC 4,请务必查看此答案.
如果您仍然收到错误:
maxJsonLength属性设置为web.config中的最大值后你的问题很可能是:
MaxJsonLength属性的值仅适用于异步通信层用于调用Web服务方法的内部JavaScriptSerializer实例.(MSDN:ScriptingJsonSerializationSection.MaxJsonLength属性)
基本上,"内部" JavaScriptSerializer尊重maxJsonLength从Web方法调用时的值; 直接使用的一个JavaScriptSerializer(或经由MVC行动的方法/控制器使用)不不尊重maxJsonLength属性,至少没有从systemWebExtensions.scripting.webServices.jsonSerializationweb.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;
Run Code Online (Sandbox Code Playgroud)
这个答案是我对这个asp.net论坛答案的解释.
小智 335
在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
};
}
Run Code Online (Sandbox Code Playgroud)
在你的控制器中.
加成:
对于任何困惑您需要指定的参数的人来说,调用可能如下所示:
Json(
new {
field1 = true,
field2 = "value"
},
"application/json",
Encoding.UTF8,
JsonRequestBehavior.AllowGet
);
Run Code Online (Sandbox Code Playgroud)
M4N*_*M4N 61
您可以在web.config文件中配置json请求的最大长度:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="....">
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Run Code Online (Sandbox Code Playgroud)
maxJsonLength的默认值是102400.有关更多详细信息,请参阅此MSDN页面:http://msdn.microsoft.com/en-us/library/bb763183.aspx
Fle*_*lea 37
我在ASP.NET Web窗体中遇到了这个问题.它完全忽略了web.config文件设置所以我这样做了:
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
return serializer.Serialize(response);
Run Code Online (Sandbox Code Playgroud)
当然总的来说这是一种可怕的做法.如果要在Web服务调用中发送这么多数据,您应该看一下不同的方法.
Rav*_*and 31
如果在web.config设置之后仍然出现错误,如下所示:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我解决了以下问题:
public ActionResult/JsonResult getData()
{
var jsonResult = Json(superlargedata, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
Run Code Online (Sandbox Code Playgroud)
我希望这应该有所帮助.
小智 22
我修好了它.
//your Json data here
string json_object="........";
JavaScriptSerializer jsJson = new JavaScriptSerializer();
jsJson.MaxJsonLength = 2147483644;
MyClass obj = jsJson.Deserialize<MyClass>(json_object);
Run Code Online (Sandbox Code Playgroud)
它工作得很好.
小智 18
我遵循了vestigal的回答并得到了这个解决方案:
当我需要将一个大的json发布到控制器中的一个动作时,我会在使用JSON JavaScriptSerializer进行反序列化时得到着名的"错误."字符串的长度超过了maxJsonLength属性上设置的值.\ r \nParameter name:input价值提供者".
我做的是创建一个新的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;
}
}
Run Code Online (Sandbox Code Playgroud)
}
然后,在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);
}
Run Code Online (Sandbox Code Playgroud)
小智 17
如果在您的web.config中实现上述添加后,您会收到"无法识别的配置部分system.web.extensions."错误,然后尝试将此添加到您的web.config中<ConfigSections>:
<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>
Run Code Online (Sandbox Code Playgroud)
小智 11
你可以把这一行写入Controller
json.MaxJsonLength = 2147483644;
Run Code Online (Sandbox Code Playgroud)
你也可以把这一行写进去 web.config
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647">
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
Run Code Online (Sandbox Code Playgroud)
`
为了安全起见,请同时使用两者.
wol*_*yuk 10
如果从MVC中的MiniProfiler收到此错误,则可以通过将属性设置MiniProfiler.Settings.MaxJsonResponseSize为所需的值来增加该值.默认情况下,此工具似乎忽略config中设置的值.
MiniProfiler.Settings.MaxJsonResponseSize = 104857600;
Run Code Online (Sandbox Code Playgroud)
小智 10
只需在MVC的Action方法中设置MaxJsonLength proprty即可
JsonResult json= Json(classObject, JsonRequestBehavior.AllowGet);
json.MaxJsonLength = int.MaxValue;
return json;
Run Code Online (Sandbox Code Playgroud)
小智 9
我建议将其设置为Int32.MaxValue.
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
Run Code Online (Sandbox Code Playgroud)
一些属性魔法怎么样?
[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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用全局筛选器配置或控制器/操作方式全局应用它.
问题实际上是您是否真的需要返回17,000条记录?您打算如何处理浏览器中的所有数据?无论如何,用户不会滚动浏览17000行。
更好的方法是仅检索“前几个”记录,并根据需要加载更多记录。
如果您在查看时遇到此类问题,可以使用以下方法来解决。这里我使用了Newtonsoft包。
@using Newtonsoft.Json
<script type="text/javascript">
var partData = @Html.Raw(JsonConvert.SerializeObject(ViewBag.Part));
</script>
Run Code Online (Sandbox Code Playgroud)
替代 ASP.NET MVC 5 修复:
(我的与上面的 MFC 答案类似,有一些小变化)
我还没有准备好更改为 Json.NET,就我而言,错误是在请求期间发生的。在我的场景中,最好的方法是修改实际的JsonValueProviderFactory,将修复应用到全局项目,并且可以通过编辑文件global.cs来完成。
JsonValueProviderConfig.Config(ValueProviderFactories.Factories);
Run Code Online (Sandbox Code Playgroud)
添加 web.config 条目:
<add key="aspnet:MaxJsonLength" value="20971520" />
Run Code Online (Sandbox Code Playgroud)
然后创建以下两个类
public class JsonValueProviderConfig
{
public static void Config(ValueProviderFactoryCollection factories)
{
var jsonProviderFactory = factories.OfType<JsonValueProviderFactory>().Single();
factories.Remove(jsonProviderFactory);
factories.Add(new CustomJsonValueProviderFactory());
}
}
Run Code Online (Sandbox Code Playgroud)
这基本上是默认实现的精确副本,System.Web.Mvc但添加了可配置的 web.config appsetting 值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;
}
}
Run Code Online (Sandbox Code Playgroud)
修复 ASP.NET MVC:如果您只想针对导致问题的特定操作修复它,请更改此代码:
public JsonResult GetBigJson()
{
var someBigObject = GetBigObject();
return Json(someBigObject);
}
Run Code Online (Sandbox Code Playgroud)
对此:
public JsonResult GetBigJson()
{
var someBigObject = GetBigObject();
return new JsonResult()
{
Data = someBigObject,
JsonRequestBehavior = JsonRequestBehavior.DenyGet,
MaxJsonLength = int.MaxValue
};
}
Run Code Online (Sandbox Code Playgroud)
并且功能应该是相同的,您可以只返回更大的 JSON 作为响应。
基于ASP.NET MVC源代码的说明:可以查看Controller.JsonASP.NET MVC源代码中的方法是什么
protected internal JsonResult Json(object data)
{
return Json(data, null /* contentType */, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
}
Run Code Online (Sandbox Code Playgroud)
它正在调用其他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
};
}
Run Code Online (Sandbox Code Playgroud)
传递contentType和contentEncoding对象在哪里null。所以基本上调用return Json(object)控制器相当于调用return new JsonResult { Data = object, JsonRequestBehavior = sonRequestBehavior.DenyGet }. 您可以使用第二种形式和参数化JsonResult。
那么当你设置MaxJsonLength属性时会发生什么(默认情况下它是空的)?它传递给JavaScriptSerializer.MaxJsonLength属性,然后调用JavaScriptSerializer.Serialize方法:
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
serializer.MaxJsonLength = MaxJsonLength.Value;
}
if (RecursionLimit.HasValue)
{
serializer.RecursionLimit = RecursionLimit.Value;
}
response.Write(serializer.Serialize(Data));
Run Code Online (Sandbox Code Playgroud)
当您不设置MaxJsonLenght序列化程序的属性时,它采用的 默认值仅为 2MB。
| 归档时间: |
|
| 查看次数: |
540459 次 |
| 最近记录: |