Resolution of ASP. NET MVC (post data) JSON request too large to deserialize (the JSON request was too large to be deserialized)

Source: Internet
Author: User

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 ({2Type: ' POST ',3URL: '/system/saveroleremodule ',4DataType: "JSON",5ContentType: "Application/json;charset=utf-8",6 data:JSON.stringify ({tree:treearr, Roleid:roleid}),7Successfunction(d) {8                         if(d > 0) {9$.popalter ({content: ' Operation succeeded! ', HIDEOKBTN:true, Btntxt: ' OK '});Ten                             //kq_show_info (' System hint ', ' Operation succeeded ', ' + '); One                         } A                     }, -Errorfunction(e) { -                             //kq_show_info (' System hint ', e.responsetext, +); the                     } -});

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: ',4Data:{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:

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

And

<system.web.extensions><Scripting><webservices><jsonserializationMaxjsonlength= "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 classjsondotnetvalueproviderfactory:valueproviderfactory2     {3         Public Overrideivalueprovider Getvalueprovider (controllercontext controllercontext)4        {5             if(ControllerContext = =NULL)6                 Throw NewArgumentNullException ("ControllerContext");7             8             if(!controllercontext.httpcontext.request.contenttype.startswith ("Application/json", stringcomparison.ordinalignorecase))9                 return NULL;Ten  One             varReader =NewStreamReader (controllerContext.HttpContext.Request.InputStream); A             varBodyText =Reader. ReadToEnd (); -  -             returnString.IsNullOrEmpty (bodyText)?NULL:Newdictionaryvalueprovider<Object> (Jsonconvert.deserializeobject<expandoobject> (BodyText,NewExpandoobjectconverter ()), cultureinfo.currentculture); the         } -}

Global.asax

1 protected voidApplication_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);9ValueProviderFactories.Factories.Remove (valueproviderfactories.factories.oftype<jsonvalueproviderfactory >(). FirstOrDefault ());TenVALUEPROVIDERFACTORIES.FACTORIES.ADD (Newjsondotnetvalueproviderfactory ()); One             //Autofacbuilder<modulesrepository>. Registerpersistent (); A}

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!

Resolution of ASP. NET MVC (post data) JSON request too large to deserialize (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.