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   // 设置为最大值,根据需要调整
            }; 
        }
    }
}
相关推荐
丰锋ff6 小时前
7.2Qt中Json的操作
开发语言·qt·json
breeze jiang7 小时前
NestJS MVC 实战:用 hello 项目 7 个文件讲清分层、装饰器、依赖注入与错误处理
node.js·mvc·js
张宇Joaquin8 小时前
高性能计算之MPI——集合通信漫谈
服务器·microsoft·asp.net
bugcome_com16 小时前
JWT 知识小课堂:从入门到精通,一文吃透 JSON Web Token
前端·json
爱敲键盘的猴子1 天前
Spring MVC 详解(一):全注解开发与请求响应处理
java·spring·mvc
ccino .1 天前
awvs_json_word.py 使用说明手册
json·word
richard_yuu1 天前
JSON vs XML vs 二进制:3种序列化终极选型
xml·c++·qt·学习·json
用户7783366132112 天前
SERP 返回 JSON 用 Pydantic 强校验:类型安全解析
json
灵析表格2 天前
灵析表格财务函数深度实用性分析与实操教程
开发语言·ai·json·excel·wps
青 春 记 忆2 天前
零基础入门python07:让程序记住数据——JSON文件和异常处理
开发语言·windows·python·json·python3.11