JavaScriptSerializer期间ASP.NET MVC中的MaxJsonLength异常

时间:2011-04-17 09:56:54

标签: asp.net-mvc exception serialization jsonresult

在我的一个控制器操作中,我返回一个非常大的JsonResult来填充网格。

我收到以下InvalidOperationException例外:

使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。

遗憾的是,将maxJsonLength中的web.config属性设置为更高的值不会产生任何影响。

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

我不想将其作为字符串传回,如this所述,回答。

在我的研究中,我发现了this博文,其中建议您自行编写ActionResult(例如LargeJsonResult : JsonResult)以绕过此行为。

这是唯一的解决方案吗? 这是ASP.NET MVC中的错误吗? 我错过了什么吗?

非常感谢任何帮助。

15 个答案:

答案 0 :(得分:203)

看来这已在MVC4中修复。

你可以做到这一点,这对我很有用:

public ActionResult SomeControllerAction()
{
  var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}

答案 1 :(得分:32)

您也可以使用ContentResult作为suggested here,而不是子类化JsonResult

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

return new ContentResult()
{
    Content = serializer.Serialize(data),
    ContentType = "application/json",
};

答案 2 :(得分:24)

不幸的是,web.config设置为ignored by the default JsonResult implementation。所以我想你需要实现一个自定义的json结果来克服这个问题。

答案 3 :(得分:20)

无需自定义课程。这就是所需要的:

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

其中Result是您要序列化的数据。

答案 4 :(得分:5)

如果使用Json.NET生成json字符串,则无需设置MaxJsonLength值。

return new ContentResult()
{
    Content = Newtonsoft.Json.JsonConvert.SerializeObject(data),
    ContentType = "application/json",
};

答案 5 :(得分:4)

我按照 link

解决了这个问题
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="5cm" height="3cm" viewBox="0 0 5 3">
  <desc>Example link01 - a link on an ellipse
  </desc>
  <rect x=".01" y=".01" width="4.98" height="2.98" fill="none" stroke="blue" stroke-width=".03" />
  <a xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.w3.org">
    <ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" />
  </a>
  <a href="#">anchor</a>
</svg>

答案 6 :(得分:4)

我很惊讶没有人建议使用结果过滤器。这是全局插入操作/结果管道的最干净的方法:

public class JsonResultFilter : IResultFilter
{
    public int? MaxJsonLength { get; set; }

    public int? RecursionLimit { get; set; }

    public void OnResultExecuting(ResultExecutingContext filterContext)
    {
        if (filterContext.Result is JsonResult jsonResult)
        {
            // override properties only if they're not set
            jsonResult.MaxJsonLength = jsonResult.MaxJsonLength ?? MaxJsonLength;
            jsonResult.RecursionLimit = jsonResult.RecursionLimit ?? RecursionLimit;
        }
    }

    public void OnResultExecuted(ResultExecutedContext filterContext)
    {
    }
}

然后,使用GlobalFilters.Filters注册该类的实例:

GlobalFilters.Filters.Add(new JsonResultFilter { MaxJsonLength = int.MaxValue });

答案 7 :(得分:2)

您可以尝试在LINQ表达式中仅定义您需要的字段。

实施例。想象一下,您有一个带有Id,名称,电话和图片(字节数组)的模型,需要从json加载到选择列表中。

LINQ查询:

var listItems = (from u in Users where u.name.Contains(term) select u).ToList();

这里的问题是&#34; 选择你&#34;获得所有领域。所以,如果你有大图片,那么booomm。

如何解决?非常非常简单。

var listItems = (from u in Users where u.name.Contains(term) select new {u.Id, u.Name}).ToList();

最佳做法是仅选择您将使用的字段。

记住。这是一个简单的提示,但可以帮助许多ASP.NET MVC开发人员。

答案 8 :(得分:2)

替代ASP.NET MVC 5修复:

在我的情况下,错误发生在请求期间。我的方案中的最佳方法是修改将修复程序应用于全局项目的实际JsonValueProviderFactory,并且可以通过编辑global.cs文件来完成。

JsonValueProviderConfig.Config(ValueProviderFactories.Factories);

添加web.config条目:

<add key="aspnet:MaxJsonLength" value="20971520" />

然后创建以下两个类

public class JsonValueProviderConfig
{
    public static void Config(ValueProviderFactoryCollection factories)
    {
        var jsonProviderFactory = factories.OfType<JsonValueProviderFactory>().Single();
        factories.Remove(jsonProviderFactory);
        factories.Add(new CustomJsonValueProviderFactory());
    }
}

这基本上是System.Web.Mvc中的默认实现的精确副本,但添加了可配置的web.config appsetting value aspnet:MaxJsonLength

public class CustomJsonValueProviderFactory : ValueProviderFactory
{

    /// <summary>Returns a JSON value-provider object for the specified controller context.</summary>
    /// <returns>A JSON value-provider object for the specified controller context.</returns>
    /// <param name="controllerContext">The controller context.</param>
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        if (controllerContext == null)
            throw new ArgumentNullException("controllerContext");

        object deserializedObject = CustomJsonValueProviderFactory.GetDeserializedObject(controllerContext);
        if (deserializedObject == null)
            return null;

        Dictionary<string, object> strs = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
        CustomJsonValueProviderFactory.AddToBackingStore(new CustomJsonValueProviderFactory.EntryLimitedDictionary(strs), string.Empty, deserializedObject);

        return new DictionaryValueProvider<object>(strs, CultureInfo.CurrentCulture);
    }

    private static object GetDeserializedObject(ControllerContext controllerContext)
    {
        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            return null;

        string fullStreamString = (new StreamReader(controllerContext.HttpContext.Request.InputStream)).ReadToEnd();
        if (string.IsNullOrEmpty(fullStreamString))
            return null;

        var serializer = new JavaScriptSerializer()
        {
            MaxJsonLength = CustomJsonValueProviderFactory.GetMaxJsonLength()
        };
        return serializer.DeserializeObject(fullStreamString);
    }

    private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, object value)
    {
        IDictionary<string, object> strs = value as IDictionary<string, object>;
        if (strs != null)
        {
            foreach (KeyValuePair<string, object> keyValuePair in strs)
                CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakePropertyKey(prefix, keyValuePair.Key), keyValuePair.Value);

            return;
        }

        IList lists = value as IList;
        if (lists == null)
        {
            backingStore.Add(prefix, value);
            return;
        }

        for (int i = 0; i < lists.Count; i++)
        {
            CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakeArrayKey(prefix, i), lists[i]);
        }
    }

    private class EntryLimitedDictionary
    {
        private static int _maximumDepth;

        private readonly IDictionary<string, object> _innerDictionary;

        private int _itemCount;

        static EntryLimitedDictionary()
        {
            _maximumDepth = CustomJsonValueProviderFactory.GetMaximumDepth();
        }

        public EntryLimitedDictionary(IDictionary<string, object> innerDictionary)
        {
            this._innerDictionary = innerDictionary;
        }

        public void Add(string key, object value)
        {
            int num = this._itemCount + 1;
            this._itemCount = num;
            if (num > _maximumDepth)
            {
                throw new InvalidOperationException("The length of the string exceeds the value set on the maxJsonLength property.");
            }
            this._innerDictionary.Add(key, value);
        }
    }

    private static string MakeArrayKey(string prefix, int index)
    {
        return string.Concat(prefix, "[", index.ToString(CultureInfo.InvariantCulture), "]");
    }

    private static string MakePropertyKey(string prefix, string propertyName)
    {
        if (string.IsNullOrEmpty(prefix))
        {
            return propertyName;
        }
        return string.Concat(prefix, ".", propertyName);
    }

    private static int GetMaximumDepth()
    {
        int num;
        NameValueCollection appSettings = ConfigurationManager.AppSettings;
        if (appSettings != null)
        {
            string[] values = appSettings.GetValues("aspnet:MaxJsonDeserializerMembers");
            if (values != null && values.Length != 0 && int.TryParse(values[0], out num))
            {
                return num;
            }
        }
        return 1000;
    }

    private static int GetMaxJsonLength()
    {
        int num;
        NameValueCollection appSettings = ConfigurationManager.AppSettings;
        if (appSettings != null)
        {
            string[] values = appSettings.GetValues("aspnet:MaxJsonLength");
            if (values != null && values.Length != 0 && int.TryParse(values[0], out num))
            {
                return num;
            }
        }
        return 1000;
    }
}

答案 9 :(得分:1)

在我将“操作”更改为[HttpPost]之前,上述方法都不适合我。 并将ajax类型设置为POST

    [HttpPost]
    public JsonResult GetSelectedSignalData(string signal1,...)
    {
         JsonResult result = new JsonResult();
         var signalData = GetTheData();
         try
         {
              var serializer = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };

            result.Data = serializer.Serialize(signalData);
            return Json(result, JsonRequestBehavior.AllowGet);
            ..
            ..
            ...

    }

ajax调用为

$.ajax({
    type: "POST",
    url: some_url,
    data: JSON.stringify({  signal1: signal1,.. }),
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        if (data !== null) {
            setValue();
        }

    },
    failure: function (data) {
        $('#errMessage').text("Error...");
    },
    error: function (data) {
        $('#errMessage').text("Error...");
    }
});

答案 10 :(得分:0)

在代码返回JsonResult对象之前,您需要手动读取配置部分。只需从web.config中读取一行:

uint8_t

确保在web.config中定义了配置元素

答案 11 :(得分:0)

这对我有用

        JsonSerializerSettings json = new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        var result = JsonConvert.SerializeObject(list, Formatting.Indented, json);
        return new JsonResult { Data = result, MaxJsonLength = int.MaxValue };

答案 12 :(得分:0)

    protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
    {
        return new JsonResult()
        {
            Data = data,
            ContentType = contentType,
            ContentEncoding = contentEncoding,
            JsonRequestBehavior = behavior,
            MaxJsonLength = Int32.MaxValue
        };
    }

是我在MVC 4中的修复程序。

答案 13 :(得分:0)

还有其他情况-数据从客户端发送到服务器。 当您使用控制器方法并且模型很大时:

    [HttpPost]
    public ActionResult AddOrUpdateConsumerFile(FileMetaDataModelView inputModel)
    {
        if (inputModel == null) return null;
     ....
    }

系统会引发类似这样的异常:“使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了在maxJsonLength属性上设置的值。参数名称:input”

在这种情况下,仅更改Web.config设置不足以提供帮助。您还可以重写mvc json序列化程序,以支持巨大的数据模型大小,或从Request中手动反序列化模型。您的控制器方法将变为:

   [HttpPost]
    public ActionResult AddOrUpdateConsumerFile()
    {
        FileMetaDataModelView inputModel = RequestManager.GetModelFromJsonRequest<FileMetaDataModelView>(HttpContext.Request);
        if (inputModel == null) return null;
        ......
    }

   public static T GetModelFromJsonRequest<T>(HttpRequestBase request)
    {
        string result = "";
        using (Stream req = request.InputStream)
        {
            req.Seek(0, System.IO.SeekOrigin.Begin);
            result = new StreamReader(req).ReadToEnd();
        }
        return JsonConvert.DeserializeObject<T>(result);
    }

答案 14 :(得分:0)

如果要从控制器返回视图,并且想在使用cshtml中的json编码时增加视图包数据的长度,可以将此代码放在cshtml中

@{
    var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
    jss.MaxJsonLength = Int32.MaxValue;
    var userInfoJson = jss.Serialize(ViewBag.ActionObj);
}

var dataJsonOnActionGrid1 = @Html.Raw(userInfoJson);

现在,dataJsonOnActionGrid1将在js页面上可用,您将获得正确的结果。

谢谢