asp.net mvc return json()设置maxJsonLength

asp.net mvc异常信息 Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

在ASP.NET MVC中,当你遇到使用JavaScriptSerializer进行JSON序列化或反序列化时出现的错误,提示"字符串长度超过了在maxJsonLength属性上设置的值",这通常意味着你正在尝试处理的数据量超过了默认的最大限制。JavaScriptSerializermaxJsonLength属性默认设置为一个相对较小的值(通常是102400字节,即100KB),这可能会导致在处理大数据集时出错。

cs 复制代码
using DS_SCZX.BLL;
using DS_SCZX.Entities.PartiallyPreparedProducts;
using DS_SCZX.Entities;
using DS_SCZX.Helps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Runtime;

namespace DS_SCZX.Controllers
{
    /// <summary>
    /// 日志
    /// </summary>
    public class Custom_LogsController : Controller
    {
        // GET: Custom_Logs
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 查询日志
        /// </summary>
        /// <param name="APIShortName"></param>
        /// <param name="APIDescribe"></param>
        /// <param name="InterfaceParameters"></param>
        /// <param name="ReturnParameters"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetCustom_LogsPageList(string APIShortName,string APIDescribe,string InterfaceParameters, string ReturnParameters, int page = -1, int limit = -1)
        {
            Custom_LogsBLL bll = new Custom_LogsBLL();
            PageData pd = bll.GetCustom_LogsPageList(APIShortName,  APIDescribe,  InterfaceParameters,  ReturnParameters, page, limit);
            var serializer = new JavaScriptSerializer();
            serializer.MaxJsonLength = int.MaxValue; // 设置为最大值,根据需要调整
            var ListModel = TableHelpher.ToListModel<Custom_LogsEntity>(pd.DataList);
          
            var result = new
            {
                code = 0,
                msg = "获取成功",
                count = pd.RowCount,
                data = ListModel
            };
            
            return new JsonResult
            {
                Data = result,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = int.MaxValue   // 设置为最大值,根据需要调整
            }; 
        }
    }
}
相关推荐
一枚小小程序员哈18 小时前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
做一位快乐的码农2 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net
陈涛5752 天前
5个最好用的 JSON 工具推荐:让数据处理变得简单高效
json
optimistic_chen2 天前
【Java EE进阶 --- SpringBoot】初识Spring(创建SpringBoot项目)
spring boot·后端·spring·java-ee·tomcat·mvc·idea
bkspiderx3 天前
pb2json.hpp 文档:Protobuf 与 JSON 通用转换工具类
json·protobuf·protobuf与json转换
万粉变现经纪人4 天前
何解决PyCharm中pip install安装Python报错ModuleNotFoundError: No module named ‘json’问题
python·pycharm·json·beautifulsoup·scikit-learn·matplotlib·pip
不知名搬运工4 天前
9 ABP Framework 中的 MVC 和 Razor Pages
mvc
一枚小小程序员哈4 天前
基于C#、.net、asp.net的心理健康咨询系统设计与实现/心理辅导系统设计与实现
c#·asp.net·.net
晨欣4 天前
orjson 与 json:实战对比与选型指南(含示例)(GPT-5 回答)
gpt·json
Archy_Wang_14 天前
ASP.NET 上传文件安全检测方案
后端·c#·asp.net