JSON, ASP.NET MVC - MaxJsonLength exception

时光怂恿深爱的人放手 提交于 2019-12-01 15:47:20

问题


I am just trying to shift some comma separated numbers to the frontend:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetSquares()
{
 var result = new JsonResult();
 result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
IList<double> list = new List<double>();
...
result.Data = list;
return result;
}

This works fine as long as there are only a few numbers. Unfortunately, I have to shift lots of numbers occasionally and get a MaxJsonLength exception. I tried several suggestions to overcome this (add something to the web.config file etc.). Maybe I do not have to use JSON after all? However I still 'have to do something' with the numbers using javascript. I am using jquery's ajax stuff at the moment.

Any suggestions welcome ...


回答1:


This won't work?

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

If not maybe you can just pass it back as a string...

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetSquares()
{
    IList<double> list = new List<double>();
    ....
    return Content(string.Join(",", list));
}



回答2:


Here is another custom JsonResult (CorrectJsonResult) which handles larger serialization limits that the default 4MB allowed by JavascriptConverter.

And another example which uses ContentResult instead of JsonResult subclass.

public ActionResult GetLargeJsonResult()
{
  return new ContentResult
  {
    Content = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue }.Serialize(myBigdata),
    ContentType = "application/json"
  };
}



回答3:


I have extended the base class Controller and work great:

ControllerExtensions class:

namespace SCAWEB.Helpers
{
    public static class ControllerExtensions
    {
        #region Json
        public static int MaxJsonLength{get;set;}

        static ControllerExtensions()
        {
            MaxJsonLength = 2147483644;
        }

        public static System.Web.Mvc.JsonResult LargeJson(this System.Web.Mvc.Controller controlador, object data)
        {
            return new System.Web.Mvc.JsonResult()
            {
                Data = data,
                MaxJsonLength = MaxJsonLength,
            };
        }
        public static System.Web.Mvc.JsonResult LargeJson(this System.Web.Mvc.Controller controlador, object data, System.Web.Mvc.JsonRequestBehavior behavior)
        {
            return new System.Web.Mvc.JsonResult()
            {
                Data = data,
                JsonRequestBehavior = behavior,
                MaxJsonLength = MaxJsonLength
            };
        }
        //TODO: You can add more overloads, the controller class has 6 overloads
        #endregion
    }
}

MyController class:

using SCAWEB.Helpers;

namespace SCAWEB.Controllers
{
    public class VentasController : Controller
    {
        public ActionResult VentasList (){
            //Todo: All the action code

            //return this.Json(myData);
            return this.LargeJson(myData);//Here I use my extensions
        }
    }
}

You can especify the max length in your code:

ControllerExtensions.MaxJsonLength = 1073741824;//1GB



回答4:


See explaination and LargeJsonResult class at http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/



来源:https://stackoverflow.com/questions/4155014/json-asp-net-mvc-maxjsonlength-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!