The Json request for ASP. net mvc (post data) is too large to be deserialized (The JSON request was too large to be deserialized ),

Source: Internet
Author: User

The Json request for ASP. net mvc (post data) is too large to be deserialized (The JSON request was too large to be deserialized ),

This problem does not occur in many scenarios. When you send an asynchronous request to the server (ajax) when the post data is very large (for example, when assigning permissions to a role during permission management, all I 've encountered is that this role has about 200 modules, and each module has two functions on average-then the action sent to the server will be an array with 400 objects)

Previously, we may need to use the asynchronous post array to the server.

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: 'operation successful! ', HideOkBtn: true, btnTxt: 'confirmed'}); 10 // kq_show_info ('System prompt', 'Operation successful ', 2000); 11} 12}, 13 error: function (e) {14 // kq_show_info ('System prompt ', e. responseText, 2000); 15} 16 });

However, after we replace javascriptserializer with json.net, the preceding method can be simplified as follows:

1 $.ajax({2 type:'post',3 url:'',4 data:{o:arr}5 success:function(d){},6 error:function(e){}7 })

 

Solution:

Solution 1.asp.net mvc the default json serialization ValueProviderFactory uses javascriptserializer, which can be set in the configuration file web. config:

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

And

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

 

Solution 2: override the default ValueProviderFactory, inherit the ValueProviderFactory abstract class, replace javascriptserializer with json.net, and remove the default ValueProviderFactory when application_start, and use the custom ValueProviderFactory

 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     }

Global. asax

 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         }

I think json.net is better than the default javascriptserializer, so the second solution is adopted!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.