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

Mar*_*erl 116 asp.net-mvc serialization exception jsonresult

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

我收到以下InvalidOperationException异常:

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

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

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="2147483644"/>
    </webServices>
  </scripting>
</system.web.extensions>
Run Code Online (Sandbox Code Playgroud)

我不想把它作为一个字符串传回去,就像这个 SO答案中提到的那样.

在我的研究中,我遇到了这篇博文,其中建议编写自己的ActionResult(例如LargeJsonResult : JsonResult)来绕过这种行为.

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

非常感激任何的帮助.

Ori*_*rds 217

看起来这已在MVC4中得到修复.

你可以这样做,这对我有用:

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


Sli*_*SFT 32

你也可以使用ContentResult建议在这里,而不是子类JsonResult.

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

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

  • 在我的情况下,在一次性应用程序上工作,这个解决方案最适合我。保存了实现 jsonresult。谢谢! (2认同)

Dar*_*rov 25

遗憾的是,默认的JsonResult实现忽略了web.config设置.所以我想你需要实现一个自定义的json结果来克服这个问题.


小智 21

不需要自定义类.这就是所需要的:

return new JsonResult { Data = Result, MaxJsonLength = Int32.MaxValue };
Run Code Online (Sandbox Code Playgroud)

Result您要序列化的数据在哪里?


Ron*_*rby 7

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

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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,使用以下命令注册该类的实例GlobalFilters.Filters

GlobalFilters.Filters.Add(new JsonResultFilter { MaxJsonLength = int.MaxValue });
Run Code Online (Sandbox Code Playgroud)


Max*_*ich 6

替代 ASP.NET MVC 5 修复:

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

JsonValueProviderConfig.Config(ValueProviderFactories.Factories);
Run Code Online (Sandbox Code Playgroud)

添加一个 web.config 条目:

<add key="aspnet:MaxJsonLength" value="20971520" />
Run Code Online (Sandbox Code Playgroud)

然后创建以下两个类

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

这基本上是在 中找到的默认实现的精确副本,System.Web.Mvc但添加了可配置的 web.config appsetting 值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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Aec*_*Liu 5

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

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


Ole*_*nko 5

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

    [HttpPost]
    public ActionResult AddOrUpdateConsumerFile(FileMetaDataModelView inputModel)
    {
        if (inputModel == null) return null;
     ....
    }
Run Code Online (Sandbox Code Playgroud)

系统抛出这样的异常“使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超出了 maxJsonLength 属性上设置的值。参数名称:输入”

在这种情况下,仅更改 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);
    }
Run Code Online (Sandbox Code Playgroud)