JavaScript | Serializer期间ASP.NET MVC中的MaxJsonLength异常

| 在我的控制器动作之一中,我返回一个很大的ѭ0来填充网格。 我收到以下“ 1”例外: 使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了在maxJsonLength属性上设置的值。 不幸的是,将
web.config
中的
maxJsonLength
属性设置为较高的值不会显示任何效果。
<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength=\"2147483644\"/>
    </webServices>
  </scripting>
</system.web.extensions>
我不想像SO答案中提到的那样将其作为字符串传递回来。 在我的研究中,我偶然发现了这篇博客文章,建议您自己写
ActionResult
(例如
LargeJsonResult : JsonResult
)来绕过此行为。 这是唯一的解决方案吗? 这是ASP.NET MVC中的错误吗? 我想念什么吗? 非常感激任何的帮助。     
已邀请:
看来这已在MVC4中修复。 您可以这样做,对我来说效果很好:
public ActionResult SomeControllerAction()
{
  var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}
    
您也可以按照此处的建议使用
ContentResult
,而不是将
JsonResult
子类化。
var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };

return new ContentResult()
{
    Content = serializer.Serialize(data),
    ContentType = \"application/json\",
};
    
不幸的是,默认的JsonResult实现忽略了web.config设置。因此,我想您将需要实现自定义json结果以克服此问题。     
无需自定义类。这就是所需要的:
return new JsonResult { Data = Result, MaxJsonLength = Int32.MaxValue };
Result
是您要序列化的数据。     
如果使用Json.NET生成
json
字符串,则无需设置
MaxJsonLength
值。
return new ContentResult()
{
    Content = Newtonsoft.Json.JsonConvert.SerializeObject(data),
    ContentType = \"application/json\",
};
    
我通过以下链接解决了这个问题
namespace System.Web.Mvc
{
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        if (controllerContext == null)
            throw new ArgumentNullException(\"controllerContext\");

        if (!controllerContext.HttpContext.Request.ContentType.StartsWith(\"application/json\", StringComparison.OrdinalIgnoreCase))
            return null;

        var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
        var bodyText = reader.ReadToEnd();

        return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()), CultureInfo.CurrentCulture);
    }
}

}

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        //Remove and JsonValueProviderFactory and add JsonDotNetValueProviderFactory
        ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
        ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
    }
    
您可以尝试在LINQ表达式中仅定义所需的字段。 例。假设您有一个具有ID,名称,电话和图片(字节数组)的模型,并且需要从json加载到选择列表中。 LINQ查询:
var listItems = (from u in Users where u.name.Contains(term) select u).ToList();
这里的问题是\“ select u \”获取所有字段。因此,如果您有大图片,请放心。 怎么解决?非常非常简单
var listItems = (from u in Users where u.name.Contains(term) select new {u.Id, u.Name}).ToList();
最佳做法是仅选择要使用的字段。 记得。这是一个简单的技巧,但可以帮助许多ASP.NET MVC开发人员。     
替代ASP.NET MVC 5修复: 就我而言,该错误是在请求期间发生的。在我的方案中,最好的方法是修改实际的ѭ19,将修复程序应用到全局项目,可以这样编辑
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());
    }
}
这基本上是在ѭ24found中找到的默认实现的精确副本,但是添加了可配置的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;
    }
}
    
在我将Action更改为
[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...\");
    }
});
    
您需要在配置代码返回JsonResult对象之前手动阅读配置部分。只需单行从web.config中读取:
        var jsonResult = Json(resultsForAjaxUI);
        jsonResult.MaxJsonLength = (ConfigurationManager.GetSection(\"system.web.extensions/scripting/webServices/jsonSerialization\") as System.Web.Configuration.ScriptingJsonSerializationSection).MaxJsonLength;
        return jsonResult;
确保在web.config中定义了配置元素     
这对我有用
        JsonSerializerSettings json = new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
        var result = JsonConvert.SerializeObject(list, Formatting.Indented, json);
        return new JsonResult { Data = result, MaxJsonLength = int.MaxValue };
    
    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中修复的问题吗?     
还有其他情况-数据从客户端发送到服务器。 当您使用控制器方法并且模型很大时:
    [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);
    }
    
如果要从控制器返回视图,并且想在使用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页面上可用,您将获得正确的结果。 谢谢     

要回复问题请先登录注册