简体   繁体   English

JsonValueProviderFactory抛出“请求太大”

[英]JsonValueProviderFactory throws “request too large”

I'm getting an exception that the JSON request was too large to be deserialized. 我收到一个例外,即JSON请求太大而无法反序列化。

It's coming from the JsonValueProviderFactory.... 它来自JsonValueProviderFactory。

The MVC App currently has a custom model binder using Json.Net which has no problem deserializing the json data. MVC App当前具有使用Json.Net的自定义模型绑定程序,反序列化json数据没有问题。 However I'm assuming the default JSON value provider is tripping up? 但是我假设默认的JSON值提供程序跳闸了吗? or has some weird limit built into it? 还是内置了一些怪异的限制?

It may be be to do with the latest release of MVC4 as when using the previous build of MVC4 there were no problems with large amounts of JSON. 这可能与最新版本的MVC4有关,因为在使用以前版本的MVC4时,使用大量JSON不会出现问题。

So, is there a way to change the setting s of the actual json value binder? 那么,有没有办法更改实际json值绑定器的设置s?

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

I get the impression it's some custom thing that turns it into a dictionary....I can't find any source code related to it or if there are any settings I can change? 我觉得这是一些自定义的东西,将其变成字典。...我找不到与之相关的任何源代码,或者是否有任何可以更改的设置?

Or is there an alternative ValueBinder I could use? 还是我可以使用替代ValueBinder?

or any other options? 或其他任何选择?

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

If you use JSON.NET for serialization/deserialization, you could substitute the default JsonValueProviderFactory with a custom one as shown in this blog post : 如果您使用JSON.NET进行序列化/反序列化,则可以使用自定义项替换默认的JsonValueProviderFactory,如本博客文章所示:

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);
    }
}

and in your Application_Start : 并在您的Application_Start

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

and if you want to stick with the default factory which uses the JavaScriptSerializer class you could adjust the maxJsonLength property in your web.config: 如果您想使用使用JavaScriptSerializer类的默认工厂,则可以在web.config中调整maxJsonLength属性:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483644"/>
        </webServices>
    </scripting>
</system.web.extensions>

For me, the maxJsonLength was not being exceeded. 对我来说,没有超过maxJsonLength。 I had to add the following: 我必须添加以下内容:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

It worked a treat after that. 在那之后它奏效了。

The above suggestions didn't work for me until I tried this: 在我尝试以下方法之前,以上建议对我不起作用:

// Global.asax.cs    
protected void Application_Start() {
    MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM