简体   繁体   English

如何在C#asp.net MVC中扩展RecursionLimit的限制

[英]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. 在ASP.NET MVC中,当我尝试通过JSON将数据发送到浏览器时,我得到了异常RecursionLimit。

how i can make it work no matter how much big JSON is. 无论JSON有多大,我怎么能让它发挥作用。

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. RecursionLimit属性的默认值为100,这意味着它可以序列化嵌套到相互引用的100个对象的深度的对象。

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. 您不能JSON序列化递归引用自身的对象。 Ayende Rahien has a nice series of blog posts about this issue. Ayende Rahien有一系列关于这个问题的精彩博文

You can JSON Serialize objects that are recursively referencing themselves as you Ignore those Properties. 您可以在忽略这些属性时JSON序列化递归引用自身的对象。 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 : 似乎Microsoft已经为JsonResult类添加了一些有用的属性,以增加.NET Framework 4.5中的递归限制和最大JSON长度:

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. 因此,现在您可以通过设置这些属性轻松设置您希望允许JSON对象的“大”。

Since the following configuration seems to be useless in ASP.NET MVC: 由于以下配置在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: 而不是使用默认递归限制为100的MVC序列化方法:

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 : 除了前面提到的方法,您可以使用Json.NET提供的默认JSON序列替换默认的JSON序列, 默认情况下似乎没有明确的限制

A null value means there is no maximum. 空值表示没有最大值。 The default value is null. 默认值为null。

Eg: 例如:

JsonConvert.SerializeObject(theObject);

NOTE: ASP.NET Web API uses this JSON serializer by default . 注意: ASP.NET Web API 默认使用此JSON序列化程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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