我只是想将一些逗号分隔的数字移到前端:
[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;
}
只要只有少数数字,这个工作正常。不幸的是,我偶尔会移动很多数字并获得MaxJsonLength异常。我尝试了几个建议来克服这个问题(在web.config文件中添加一些内容等)。也许我毕竟不必使用JSON?但是我仍然需要使用javascript对数字做些什么。我现在正在使用jquery的ajax。
欢迎任何建议......
答案 0 :(得分:5)
Here is another custom JsonResult
(CorrectJsonResult
)处理JavascriptConverter
允许的默认4MB的较大序列化限制。
And another example which uses ContentResult
代替JsonResult
子类。
public ActionResult GetLargeJsonResult()
{
return new ContentResult
{
Content = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue }.Serialize(myBigdata),
ContentType = "application/json"
};
}
答案 1 :(得分:4)
我扩展了基类Controller并且运行得很好:
ControllerExtensions类:
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类:
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
}
}
}
您可以在代码中指定最大长度:
ControllerExtensions.MaxJsonLength = 1073741824;//1GB
答案 2 :(得分:3)
请参阅explainination和LargeJsonResult类 http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/
答案 3 :(得分:1)
这不起作用?
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
如果没有,也许你可以把它作为一个字符串传回来......
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetSquares()
{
IList<double> list = new List<double>();
....
return Content(string.Join(",", list));
}