JSON, ASP.NET MVC - MaxJsonLength exception JSON, ASP.NET MVC - MaxJsonLength 异常
JSON, ASP.NET MVC - MaxJsonLength exception JSON, ASP.NET MVC - MaxJsonLength 异常
我只是想将一些逗号分隔的数字转移到前端:
[AcceptVerbs(HttpVerbs.Get)] public JsonResult GetSquares() { var result = new JsonResult(); result.JsonRequestBehavior = JsonRequestBehavior.AllowGet; IListlist = new List (); ... result.Data = list; return result; }
只要数字不多,这个方法就能正常工作。不幸的是,我偶尔需要转移大量数字,但会遇到MaxJsonLength异常。我尝试了几个解决方法(例如在web.config文件中添加一些内容等)。也许我不必使用JSON?但是我仍然需要使用JavaScript处理这些数字。目前我正在使用jQuery的ajax功能。
欢迎任何建议...
ASP.NET MVC是一种用于构建Web应用程序的框架。在使用ASP.NET MVC开发过程中,有时候会遇到一个名为"MaxJsonLength exception"的问题。这篇文章将详细解释这个问题的原因以及如何解决。
"MaxJsonLength exception"问题通常在返回较大的JSON数据时出现。ASP.NET MVC默认设置了一个最大的JSON长度限制,当返回的JSON数据超过这个限制时,就会抛出"MaxJsonLength exception"异常。
解决这个问题的方法是自定义一个JsonResult类,通过增加最大JSON长度来避免异常的抛出。下面是一个示例的解决方法:
public class LargeJsonResult : JsonResult { public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("GET request not allowed"); } HttpResponseBase response = context.HttpContext.Response; if (!String.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Data != null) { JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; // Increase the maximum JSON length here response.Write(serializer.Serialize(Data)); } } }
使用这个自定义的LargeJsonResult类,可以避免"MaxJsonLength exception"异常的抛出。只需要将原来返回JsonResult的地方改为返回LargeJsonResult即可。
更详细的解释和LargeJsonResult类的实现可以参考这篇文章:http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/
在ASP.NET MVC中,当我们返回大量数据时,可能会遇到一个名为"MaxJsonLength exception"的异常。这个异常的原因是默认的JSON序列化器在序列化过程中,超过了最大长度限制。
为了解决这个问题,我们可以通过以下步骤来设置最大JSON长度:
1. 创建一个静态类`ControllerExtensions`,扩展默认的`Controller`类。在这个类中,我们定义了一个名为`MaxJsonLength`的属性,用于设置最大JSON长度。默认值为2147483644。
namespace SCAWEB.Helpers { public static class ControllerExtensions { public static int MaxJsonLength { get; set; } static ControllerExtensions() { MaxJsonLength = 2147483644; } public static System.Web.Mvc.JsonResult LargeJson(this System.Web.Mvc.Controller controller, object data) { return new System.Web.Mvc.JsonResult() { Data = data, MaxJsonLength = MaxJsonLength, }; } public static System.Web.Mvc.JsonResult LargeJson(this System.Web.Mvc.Controller controller, object data, System.Web.Mvc.JsonRequestBehavior behavior) { return new System.Web.Mvc.JsonResult() { Data = data, JsonRequestBehavior = behavior, MaxJsonLength = MaxJsonLength }; } } }
2. 在我们的控制器中,我们可以使用上述扩展方法来返回大JSON数据。在我们的示例中,我们使用了一个名为`VentasController`的控制器,其中的`VentasList`方法返回了大量的数据。
using SCAWEB.Helpers; namespace SCAWEB.Controllers { public class VentasController : Controller { public ActionResult VentasList() { // TODO: 执行一些操作获取数据 // return this.Json(myData); // 使用我们的扩展方法 return this.LargeJson(myData); } } }
3. 最后,我们可以在代码中指定最大JSON长度。例如,以下代码将最大长度设置为1GB(1073741824字节):
ControllerExtensions.MaxJsonLength = 1073741824;
通过以上步骤,我们可以解决"MaxJsonLength exception"异常,并成功返回大JSON数据。
在ASP.NET MVC中,当使用默认的JsonResult类进行JSON序列化时,可能会遇到MaxJsonLength exception的问题。这个问题的出现是因为默认情况下,JsonResult使用的JavascriptConverter只允许最大4MB的JSON序列化。
为了解决这个问题,可以使用自定义的JsonResult或ContentResult来处理较大的序列化限制。下面是两个解决方法的示例代码:
1. 使用自定义的JsonResult(CorrectJsonResult):
public class CorrectJsonResult : JsonResult { public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("GET request not allowed"); } HttpResponseBase response = context.HttpContext.Response; response.ContentType = !String.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json"; if (this.ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; } if (this.Data != null) { JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; response.Write(serializer.Serialize(this.Data)); } } }
2. 使用ContentResult替代JsonResult:
public ActionResult GetLargeJsonResult() { return new ContentResult { Content = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue }.Serialize(myBigdata), ContentType = "application/json" }; }
通过使用这两种解决方法,可以处理较大的JSON序列化限制,避免出现MaxJsonLength exception的问题。