How to extend RecursionLimit limit in C # asp.net MVC

in ASP.NET MVC I got an exception that exceeded RecursionLimit when I try to send data to the browser via JSON.

how can i make it work no matter how big the json is.

+5
source share
6 answers

The default value for the property RecursionLimitis 100, which means that it can serialize objects nested at a depth of 100 objects that reference each other.

, , , , , , , , .

+10

, . JSON , . Ayende Rahien .

+4

JSON Serialize, , . My Users , , :

    Users oSuperior;
    [ScriptIgnore(), XmlIgnore()]
    public Users Superior
    {
        get
        {

            if (oSuperior == null)
            {
                oSuperior = new Users();

                if (iIDUserSuperior > 0)
                    oSuperior.Load(iIDUserSuperior);
            }
            return oSuperior;
        }
    }
+1

, Microsoft JsonResult Class JSON .NET framework 4.5:

public ActionResult GetJson()
{
    var result = Json(obj);
    result.RecursionLimit = 1024;
    result.MaxJsonLength = 8388608;
    return result;
}

, , "" JSON, .

+1

ASP.NET MVC:

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization recursionLimit="1024"/>
    </webServices>
  </scripting>
</system.web.extensions>

:

new System.Web.Script.Serialization.JavaScriptSerializer(){RecursionLimit = 1024}.Serialize(@Model)

MVC, 100:

Json.Encode(@Model)

, , .

0

In addition to the above methods, you can replace the default JSON serialization with the one provided by Json.NET, which by default does not have an explicit restriction :

A value of zero means that there is no maximum. The default value is null.

eg:.

JsonConvert.SerializeObject(theObject);

NOTE. ASP.NET Web API uses this default JSON serializer .

0
source

All Articles