The JSON request was too large to be deserialized

这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理的时候给某个角色分配权限那么就可能会出现,我所遇到的就是该角色大概200个模块每个模块平均2个功能----那么发送到服务端action的将是一个有着400个对象的数组)

之前我们向服务端异步post数组可能需要使用

The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized
 1 $.ajax({
 2                     type: 'POST',
 3                     url: '/system/SaveRoleReModule',
 4                     dataType: "json",
 5                     contentType: "application/json;charset=utf-8",
 6                     data: JSON.stringify({ tree: treearr, roleId: roleid }),
 7                     success: function (d) {
 8                         if (d > 0) {
 9                             $.popAlter({ content: '操作成功!', hideOkBtn:true,btnTxt:'确定'});
10                             // kq_show_info('系统提示', '操作成功', 2000);
11                         }
12                     },
13                     error: function (e) {
14                             //kq_show_info('系统提示', e.responseText, 2000);
15                     }
16                 });
The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized

但是当我们把javascriptserializer换成json.net之后以上方式将可以简化为以下写法:

The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized
1 $.ajax({
2 type:'post',
3 url:'',
4 data:{o:arr}
5 success:function(d){},
6 error:function(e){}
7 })
The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized

 

解决方法:

方案1.asp.net mvc默认的json序列化ValueProviderFactory使用的是javascriptserializer,可以在配置文件web.config中设置:

<add key="aspnet:MaxJsonDeserializerMembers" value="150000000" />

The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized

 

方案2:重写默认的ValueProviderFactory,继承ValueProviderFactory抽象类使用json.net替换javascriptserializer,并且在application_start时将默认的ValueProviderFactory移除,使用自定义的ValueProviderFactory

The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized
 1 public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
 2     {
 3        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
 4        {
 5             if (controllerContext == null)
 6                 throw new ArgumentNullException("controllerContext");
 7             
 8             if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
 9                 return null;
10 
11             var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
12             var bodyText = reader.ReadToEnd();
13 
14             return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
15         }
16     }
The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized

global.asax

The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized
 1 protected void Application_Start()
 2         {
 3             log4net.Config.XmlConfigurator.Configure();
 4             AreaRegistration.RegisterAllAreas();
 5             GlobalConfiguration.Configure(WebApiConfig.Register);
 6             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 7             RouteConfig.RegisterRoutes(RouteTable.Routes);
 8             BundleConfig.RegisterBundles(BundleTable.Bundles);
 9             ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
10             ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
11             //AutofacBuilder<ModulesRepository>.RegisterPersistent();
12         }
The JSON request was too large to be deserialized
    

The JSON request was too large to be deserialized

网上介绍最多的是第一种方案,但是我觉得json.net比默认的javascriptserializer性能要好所以采用第二种方案!

http://www.cnblogs.com/zpc870921/p/4798640.html

相关文章:

  • 2021-05-22
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-30
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
相关资源
相似解决方案