2011-04-17 119 views
97

在我的一个控制器操作中,我返回一个非常大的JsonResult以填充网格。JavaScriptSerializer期间ASP.NET MVC中的MaxJsonLength异常

我正在以下InvalidOperationException例外:

错误序列化或反序列化过程中使用JSON JavaScriptSerializer。字符串的长度超过maxJsonLength属性中设置的值。

web.config中的maxJsonLength属性设置为更高的值,遗憾的是没有显示任何效果。

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

我不想传回的字符串作为this提到的SO回答。

在我的研究横跨this博客文章来的地方写一个自己的ActionResult(例如LargeJsonResult : JsonResult)建议绕过此行为。

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

任何帮助将不胜感激。

+2

你的解决方案,适用于MVC 3 – MatteoSp 2013-04-09 19:31:53

+1

@Matteo确定配置元素?这是一个古老的问题,我不记得但显然我把它标记为MVC3。不幸的是,当它修复/关闭时,我无法看到版本/日期:https://aspnet.codeplex.com/workitem/3436 – 2013-04-10 04:35:08

+1

当然,我正在使用MVC 3,它的工作原理。幸运的是,因为在MVC 3中,您没有在接受的答案中引用的“MaxJsonLength”属性。 – MatteoSp 2013-04-11 10:41:59

回答

174

看来这已在MVC4中修复。

你可以做到这一点,这对我来说效果很好:

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

很好的答案!谢谢! – 2013-01-04 15:17:09

+0

我将json字符串设置到ViewBag.MyJsonString属性中,但在运行时在我的视图中出现了与以下javascript行相同的错误: var myJsonObj = @ Html.Raw(Json.Encode(ViewBag.MyJsonString)); – 2013-09-05 12:13:52

+1

嘿@orionedvards,@ GG,@ MartinBuberl我面临着同样的问题maxJson但发布数据时,控制器,我怎么能解决这个问题,我花了这么多的时间来搜索这个。任何帮助,将不胜感激。 – katmanco 2015-03-03 22:37:30

31

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

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

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

在我的情况下,工作在一次性应用程序上,这个解决方案对我来说最合适。保存实现jsonresult。谢谢! – Christo 2012-09-15 22:35:35

18

无需自定义类。这就是需要的全部内容:

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

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

+0

错误“System.Web.Mvc.JsonResult”不包含“MaxJsonLength” – PUG 2013-06-17 20:49:48

+0

你在哪里得到这个错误的定义? – John 2013-06-18 01:07:53

+0

c#.net mvc3 ...... – PUG 2013-06-18 04:45:16

2

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

例子。想象一下,你有一个模型与Id,名称,电话和图片(字节数组),并需要从json加载到选择列表。

LINQ查询:

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

这里的问题是 “选择ü” 是让所有领域。所以,如果你有大照片,booomm。

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

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

最佳实践是只选择您将使用的字段。

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

+0

我不认为在这种情况下,用户想要过滤他们的数据。有些人需要从数据库中取回大量的行...... – 2016-05-31 16:31:12

4

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

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

我按照这个link

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()); 
    } 
0

你需要从配置部分之前手动代码返回一个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中