Hello everyone
I have asp.net mvc project
When I call function from ajax that get big data I set
maxjsonlength in c# controller and it works fine
but when i wont to save that data and wont call function with big object parameter i Get error that can not deserialize big json data
it use javascriptserializer and there where default value
how change it from ajax call

Prasad RaveendranRank #320 · 7.8K reputationPosted Apr 27, 2024, 2:03 PM EDT
To handle large JSON data in your ASP.NET MVC project when sending it from JavaScript to your controller action, you need to make sure that both the client-side and server-side configurations are set appropriately.
At client side , by using
JSON.stringify(jsonData), you're converting your JSON object to a string before sending it.At Server Side, consider increasing the
maxJsonLengthproperty in yourWeb.configfile.In your
Web.config, add or modify the following entry under:To use
JsonSerializerinstead ofJavaScriptSerializerin your controller action, you can make use of theSystem.Text.Jsonnamespace which provides more modern and efficient JSON serialization/deserialization capabilitiesusing System.Text.Json; public ActionResult YourAction(string jsonData) { YourModel model = JsonSerializer.Deserializeif you could share your codesnoppet, which would help us to take a look and suggest
Karlen Sahakyan10 reputationPosted Apr 27, 2024, 5:47 PM EDT
For example in this call I get maxjsonlength error
but in server side I config maxjsonlength
$.ajax({ url : ‘test/savedata’ type : 'post', data : { 'data' : json.stringify(bigdata) }, dataType:'json' });Prasad RaveendranRank #320 · 7.8K reputationPosted Apr 28, 2024, 12:10 AM EDT
For ASP.NET MVC applications, you can handle large JSON data differently. You can send large JSON data as a string and deserialize it on the server-side. Here's how you can modify your AJAX call to send the data as a string:
$.ajax({ url: 'test/savedata', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify({ 'data': JSON.stringify(bigdata) }), dataType: 'json', success: function(response) { // Handle success }, error: function(xhr, status, error) { // Handle error } });In the controller action (
test/savedata), receive the JSON data as a string and deserialize it:public ActionResult SaveData(string data) { // Deserialize the JSON string to your object YourDataType bigData = JsonConvert.DeserializeObjectJignesh KumarRank #91 · 22.9K reputationPosted Apr 28, 2024, 6:39 AM EDT
Hello Karlen,
Please refer to this one,
$.ajax({ url: 'YourController/YourAction', type: 'POST', data: JSON.stringify(yourData), contentType: 'application/json', dataType: 'json', maxJsonLength: 5000000, // Set your desired maxJsonLength success: function (data) { // Handle success }, error: function (xhr, status, error) { // Handle error } });if you are using MVC application then please do needful in your start up method,
protected void Application_Start() { // Other configurations... var serializerSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings; serializerSettings.MaxJsonLength = int.MaxValue; // Set your desired maximum Json Length }Jaimin ShethiyaRank #177 · 13.7K reputationPosted Apr 29, 2024, 12:24 AM EDT
Hello Karlen,
Add below code in your web.config file.
Or refer below links
https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config
https://stackoverflow.com/questions/12750689/setting-jsonserialization-maxjsonlength-in-asp-net-web-config-gives-500-error
https://stackoverflow.com/questions/63332780/how-do-i-increase-size-of-maxjsonlength-for-web-api-method-input-parameter
Thanks