简体   繁体   中英

how to extend the limit of RecursionLimit in C# asp.net MVC

in ASP.NET MVC i got the exception RecursionLimit exceeded when i try to sent the data to browser through JSON.

how i can make it work no matter how much big JSON is.

The default value of the RecursionLimit property is 100, which means that it can serialise objects that are nested to a depth of 100 objects referencing each other.

I can't imagine that you actually have objects that are nested that deep, so the most likely reason for the error message is that you have an object that contains a reference to itself, or two objects that have references to each other. As that causes an infinite chain of child objects, you get the equivalent of a stack overflow in the serialiser.

Use view models to break the recursion. You cannot JSON serialize objects that are recursively referencing themselves. Ayende Rahien has a nice series of blog posts about this issue.

You can JSON Serialize objects that are recursively referencing themselves as you Ignore those Properties. My Users Class has a Property that also is a User Object, but I ignore that Property doing:

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

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

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

It seems that Microsoft has added some helpful properties to the JsonResult Class for increasing the recursion limit and maximum JSON length in the .NET framework 4.5 :

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

Hence, now you can easily set how "big" you want to allow your JSON objects to be by setting those properties.

Since the following configuration seems to be useless in ASP.NET MVC:

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

I ended up using the following one liner to solve my problem:

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

Instead of using MVC's serialization method which has a default recursion limit of 100:

Json.Encode(@Model)

It may not be the best solution, but it sure is simple to use in case you need a quick fix.

Besides aforementioned methods, you can replace the default JSON serialization with that provided by Json.NET which by default does not seem to have an explicit limit :

A null value means there is no maximum. The default value is null.

Eg:

JsonConvert.SerializeObject(theObject);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM