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);).
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!
//Masala
Kommentaar kirjutas Masala — 2013-06-14 @ 10:49:01 |
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 |
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 |
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 |
Thank for sharing. I was searching exactly this one and very helpful to me
Kommentaar kirjutas Selman — 2017-03-11 @ 15:41:48 |
How and where to call the MyFancyMethod(); ?
Kommentaar kirjutas test — 2018-02-08 @ 20:43:57 |
[…] 클래 싱 대신 여기에 제안 된ContentResult 대로 사용할 수도 있습니다 […]
Pingback-viide kirjutas [asp.net-mvc] JavaScriptSerializer 중 ASP.NET MVC의 MaxJsonLength 예외 - 리뷰나라 — 2021-08-30 @ 01:24:45 |