我收到一个异常,即JSON请求太大而无法反序列化。
它来自JsonValueProviderFactory ....
MVC App目前有一个使用Json.Net的自定义模型绑定器,对于反序列化json数据没有问题。但是我假设默认的JSON值提供程序正在绊倒?或者内置了一些奇怪的限制?
可能与最新版本的MVC4有关,因为使用之前的MVC4版本时,大量的JSON没有问题。
那么,有没有办法改变实际json值绑定器的设置?
我得到的印象是,它是一些自定义的东西,把它变成一个字典....我找不到任何与它相关的源代码或者是否有任何设置我可以改变?
或者我可以使用另一种ValueBinder吗?
或任何其他选项?
Server Error in '/' Application.
The JSON request was too large to be deserialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The JSON request was too large to be deserialized.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: The JSON request was too large to be deserialized.]
System.Web.Mvc.EntryLimitedDictionary.Add(String key, Object value) +464621
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +413
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +373
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +116
System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +34
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +177
答案 0 :(得分:68)
如果您使用JSON.NET进行序列化/反序列化,您可以将默认的JsonValueProviderFactory替换为自定义的,如this blog post所示:
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
var bodyText = reader.ReadToEnd();
return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
}
}
和您的Application_Start
:
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
如果你想坚持使用JavaScriptSerializer类的默认工厂,你可以调整web.config中的maxJsonLength
属性:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
答案 1 :(得分:64)
对我来说,没有超过maxJsonLength。我不得不添加以下内容:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
之后它就开始了。
答案 2 :(得分:3)
在我尝试之前,上述建议对我没有用处:
// Global.asax.cs
protected void Application_Start() {
MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
}