Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In one of my controller actions I am returning a very large JsonResult to fill a grid.

I am getting the following InvalidOperationException exception:

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

Setting the maxJsonLength property in the web.config to a higher value unfortunately does not show any effect.

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="2147483644"/>
    </webServices>
  </scripting>
</system.web.extensions>

I don't want to pass it back as a string as mentioned in this SO answer.

In my research I came across this blog post where writing an own ActionResult (e.g. LargeJsonResult : JsonResult) is recommended to bypass this behaviour.

Is this then the only solution?
Is this a bug in ASP.NET MVC?
Am I missing something?

Any help would be most appreciated.

share|improve this question
Your solutions works on MVC 3. – MatteoSp Apr 9 at 19:31
@Matteo Are you sure? This is an old question and I can't remember but apparently I tagged it as MVC3. Unfortunately I can't see the version/date when it got fixed/closed: aspnet.codeplex.com/workitem/3436 – Martin Buberl Apr 10 at 4:35
Sure, I'm working with MVC 3 and it works. And fortunately, because in MVC 3 you don't have the "MaxJsonLength" properties cited in the accepted answer. – MatteoSp Apr 11 at 10:41

4 Answers

up vote 12 down vote accepted

It appears this has been fixed in MVC4.

You can do this, which worked well for me:

public ActionResult SomeControllerAction()
{
  var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}
share|improve this answer
1  
Great answer! thank you! – GG. Jan 4 at 15:17

Unfortunately the web.config setting is ignored by the default JsonResult implementation. So I guess you will need to implement a custom json result to overcome this issue.

share|improve this answer

You could also use ContentResult as suggested here instead of subclassing JsonResult.

var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };

return new ContentResult()
{
    Content = serializer.Serialize(data),
    ContentType = "application/json",
};
share|improve this answer
1  
in my case, working on a throwaway app, this solution worked best for me. saved implementing jsonresult. thanks! – Christo Sep 15 '12 at 22:35

No need for a custom class. This is all that is needed:

return new JsonResult { Data = Result, MaxJsonLength = Int32.MaxValue };

where Result is that data you wish to serialize.

share|improve this answer
Error 137 'System.Web.Mvc.JsonResult' does not contain a definition for 'MaxJsonLength' – jaminator Jun 17 at 20:49
Where are you getting this error? – John Jun 18 at 1:07
c#.net mvc3...... – jaminator Jun 18 at 4:45
I know it works on MVC4. I will check MVC3. – John Jun 18 at 16:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.