The JSON request was too large to be deserialized

Source: Internet
Author: User
Tags httpcontext

The JSON request was too large to be deserialized

This problem is not a lot of scenarios, when you go to the server-side asynchronous (Ajax) post data is very large (for example, when doing permission management to assign permissions to a role, it may appear, All I'm going through is the role of about 200 modules each module has an average of 2 functions----then the action that is sent to the server will be an array of 400 objects)

Before we send an asynchronous post array to the server, you may need to use

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 succeeded!) ', Hideokbtn:true,btntxt: ' OK '});                             //Kq_show_info (' System prompt ', ' Operation successful ', $);                         }12                     },13                     Error: Function (e) {                             //kq_show_info (' System hint ', e.responsetext, +);                     }16                 });

But when we change the JavaScriptSerializer to Json.NET, the above method can be simplified to the following wording:

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

Workaround:

Scenario 1.asp.net MVC The default JSON serialization Valueproviderfactory is using JavaScriptSerializer, which can be set in the config file Web. config:

<add key= "Aspnet:maxjsondeserializermembers" value= "150000000"/>

And

<system.web.extensions><scripting><webservices><jsonserialization maxJsonLength= " 2147483644 "/></webservices></scripting></system.web.extensions>

Scenario 2: Overriding the default Valueproviderfactory, inheriting the Valueproviderfactory abstract class using json.net to replace JavaScriptSerializer, and in Application_ Start when the default valueproviderfactory is removed, using a 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 one             var reader = new StreamReader ( ControllerContext.HttpContext.Request.InputStream);             var bodyText = reader. ReadToEnd ();             string.isnullorempty return (bodyText)? Null:new dictionaryvalueprovider<object> ( Jsonconvert.deserializeobject<expandoobject> (BodyText, New Expandoobjectconverter ()), CultureInfo.CurrentCulture);         }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 ());             ValueProviderFactories.Factories.Add (New Jsondotnetvalueproviderfactory ());             Autofacbuilder<modulesrepository>. Registerpersistent ();         

The most on-line introduction is the first scenario, but I think json.net is better than the default JavaScriptSerializer performance, so use the second solution!

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

The JSON request was too large to be deserialized

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.