JsonValueProviderFactory throws “request too large”

前端 未结 3 794
长发绾君心
长发绾君心 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(JsonConvert.DeserializeObject(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
        }
    }
    
    
    

    and in your Application_Start:

    ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType().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:

    
        
            
                
            
        
    
    

    提交回复
    热议问题