…meie igapäevast IT’d anna meile igapäev…


ASP.NET MVC: JSON string too long – a solution

Filed under: ASP.NET MVC — Sander @ 11:33:53
Tags: , ,


If you get the InvalidOperationException:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

when trying to serialize data to JSON representation, then most likely you are doing something wrong. AJAX is not meant to move large amounts of data – rethink your application!

However, sometimes the limit is too low – in my case, a web service test application for the client, so they could test their own web services before claiming they are ready. One of the services gives quite a lot of data – and the test app needed to show the data as XML as well, so the actual JSON size was 26MB (as a sidenote, both Chrome and Firefox 4 crashed while parsing the data – surprisingly, IE9 was the only browser which didn’t).

I tried the Web.config approach described on various pages (1, 2), but that didn’t help. Then I realized – they are talking about “plain” ASP.NET, not ASP.NET MVC. So, I tried not returning Json(myBigData), but created my own serializer, setting the data size limit to int.MaxValue (var serializer = new JavaScriptSerializer {MaxJsonLength = Int32.MaxValue};) and then used it to serialize the return data: return new JsonResult { Data = serializer.Serialize(myBigData) };

But I still got the same exception. Apparently the JsonResult does checking of its own – so, the actual solution is not to return JsonResult, but ContentResult:

public ActionResult MyFancyMethod()
	var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };
	var result = new ContentResult
		Content = serializer.Serialize(myBigData),
		ContentType = "application/json"
	return result;

And there, 26MB returned as JSON without any issues. An alternative approach would be to write the JSON directly to the output (Response.Clear(); Response.ContentType = “application/json”; Response.Write(serializer.Serialize(myBigData);).


5 kommentaari »

  1. Hi there,
    It seems useful but as I am new in MVC and have this problem. I donot know haw i can send myBigData to MyFancyMethod().! where i should call MyFancyMethod().
    I have data an script in a view.
    Thank you in advance!
    I do really appreciate your help!


    kommentaar kirjutas Masala — 2013-06-14 @ 10:49:01 | Vasta

    • I am not sure I understand your problem, could you please clarify it?

      Sending (any) data to a MVC controller method from web page/script is the same, no matter how much data there is. If you use AJAX, you should POST the data, though.

      kommentaar kirjutas Sander — 2013-06-14 @ 11:10:36 | Vasta

  2. MyFancyMethod() looks like a server-side function. I would say this seems an odd solution since it’s during an AJAX call from JavaScript (client-side) that sends this data server-side where I get the error – and probably most people. So how can you send data that is too long in the first place, to be serialized on the server that it cannot get to?

    kommentaar kirjutas vapcguy — 2015-02-12 @ 04:58:14 | Vasta

    • Yes, of course MyFancyMethod() is server side. The whole post discusses the issues of large amounts of data on server-side – the problem was posting information from ASP.NET MVC _to_ client-side.

      kommentaar kirjutas Sander — 2015-02-12 @ 11:07:15 | Vasta

  3. Thank for sharing. I was searching exactly this one and very helpful to me

    kommentaar kirjutas Selman — 2017-03-11 @ 15:41:48 | Vasta

RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Muuda )

Twitter picture

You are commenting using your Twitter account. Log Out / Muuda )

Facebook photo

You are commenting using your Facebook account. Log Out / Muuda )

Google+ photo

You are commenting using your Google+ account. Log Out / Muuda )

Connecting to %s

Create a free website or blog at WordPress.com.