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   // 设置为最大值,根据需要调整
            }; 
        }
    }
}
相关推荐
小旭95272 小时前
Spring MVC :从入门到精通(下)
java·后端·spring·mvc
bugcome_com7 小时前
【ASP.NET Web Pages】页面布局核心实战:从复用性到安全性,打造一致化网站界面
前端·后端·asp.net
ID_180079054738 小时前
小红书笔记详情 API 接口系列 + 标准 JSON 返回参考(完整版)
数据库·笔记·json
希望永不加班9 小时前
如何在 SpringBoot 里自定义 Spring MVC 配置
java·spring boot·后端·spring·mvc
稻草猫.9 小时前
MyBatis进阶:动态SQL与MyBatis Generator插件使用
java·数据库·后端·spring·mvc·mybatis
PascalMing10 小时前
告别 Nginx!ASP.NET Core 实现多域名 Vue 静态服务与代理转发
vue.js·nginx·asp.net
鬼蛟10 小时前
Spring MVC
java·spring·mvc
鬼蛟10 小时前
Spring_MVC
java·spring·mvc
小狗丹尼40011 小时前
JSON 基础认知、数据转换与 Flask 前后端交互全解
python·flask·json
奔跑的呱呱牛1 天前
arcgis-to-geojson双向转换工具库
arcgis·json