JsonValueProviderFactory throws “request too large”

前端 未结 3 792
长发绾君心
长发绾君心 2020-11-29 05:25

I\'m getting an exception that the JSON request was too large to be deserialized.

It\'s coming from the JsonValueProviderFactory....

The MVC App currently

相关标签:
3条回答
  • 2020-11-29 05:30

    If you use JSON.NET for serialization/deserialization, you could substitute the default JsonValueProviderFactory with a custom one as shown in 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);
        }
    }
    

    and in your 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:

    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="2147483644"/>
            </webServices>
        </scripting>
    </system.web.extensions>
    
    0 讨论(0)
  • 2020-11-29 05:45

    The above suggestions didn't work for me until I tried this:

    // Global.asax.cs    
    protected void Application_Start() {
        MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
    }
    
    0 讨论(0)
  • 2020-11-29 05:57

    For me, the maxJsonLength was not being exceeded. I had to add the following:

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

    It worked a treat after that.

    0 讨论(0)
提交回复
热议问题