我收到一个异常,JSON 请求太大而无法反序列化。
它来自 JsonValueProviderFactory....
MVC 应用程序目前有一个使用 Json.Net 的自定义模型绑定(bind)器,它可以毫无问题地反序列化 json 数据。但是我假设默认的 JSON 值提供程序出错了?还是内置了一些奇怪的限制?
这可能与最新版本的 MVC4 有关,因为在使用之前版本的 MVC4 时,大量 JSON 没有问题。
那么,有没有办法改变实际的json value binder的设置呢?
我的印象是将它变成字典的是一些自定义的东西....我找不到任何与之相关的源代码,或者是否有任何我可以更改的设置?
或者是否有我可以使用的替代 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
最佳答案
如果您使用 JSON.NET 进行序列化/反序列化,您可以将默认的 JsonValueProviderFactory 替换为自定义的 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 的默认工厂你可以调整类 maxJsonLength
web.config 中的属性:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
关于c# - JsonValueProviderFactory 抛出 "request too large",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509721/