c# - JsonValueProviderFactory 抛出 "request too large"

标签 c# asp.net-mvc json.net asp.net-mvc-4

我收到一个异常,JSON 请求太大而无法反序列化。

它来自 JsonValueProviderFactory....

MVC 应用程序目前有一个使用 Json.Net 的自定义模型绑定(bind)器,它可以毫无问题地反序列化 json 数据。但是我假设默认的 JSON 值提供程序出错了?还是内置了一些奇怪的限制?

这可能与最新版本的 MVC4 有关,因为在使用之前版本的 MVC4 时,大量 JSON 没有问题。

那么,有没有办法改变实际的json value binder的设置呢?

路过http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

我的印象是将它变成字典的是一些自定义的东西....我找不到任何与之相关的源代码,或者是否有任何我可以更改的设置?

或者是否有我可以使用的替代 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/

相关文章:

jquery - 如何让我的 TreeView 在进入子页面后显示数据?

c# - MVC C# 在不删除数据库的情况下更改数据库结构

c# - 将转换器应用于 Json 数组中的所有元素

c# - 当我想完全按照没有添加 ISerializable 时它会做什么时,如何在具有 [Serializable] 的类上实现 ISerializable?

c# - WPF:从 XAML 设置 ViewModel,为什么第二个解决方案不起作用?

Asp.Net MVC 身份验证 Cookie 冲突

c# - Unity3d FullInspector 和 Json.net 未序列化数据

c# - 在类级别配置 Json.NET 序列化设置

c# - 在 SharpDX/DirectWrite 中获取 ABC 字符宽度

c# - 从 Shape 派生的 WPF 自定义形状类