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   // 设置为最大值,根据需要调整
            }; 
        }
    }
}
相关推荐
灯澜忆梦19 小时前
Go 语言 _JSON---序列化与反序列化
开发语言·golang·json
sun03222 天前
【笔记】Spring MVC 相关介绍
java·spring·mvc
梦幻通灵2 天前
Notepad++格式化Json两种方案【持续更新】
android·json·notepad++
解局易否结局2 天前
鸿蒙原生开发实战|Native JSON 编解码与高性能数据序列化
华为·json·harmonyos
不羁的木木2 天前
HarmonyOS技术精讲-Connectivity Kit:Wi-Fi进阶能力——热点、P2P与Aware
asp.net·harmonyos·p2p
Qredsun2 天前
RAG 系统 Embedding 流程分析与自托管 API 适配
mvc·embedding
代码的小搬运工2 天前
网络请求(NSURL、NSURLRequest、NSURLSessionDataTask、协议回调与 JSON 数据的基本流向)
网络·数据库·json
2601_960906723 天前
网络防御等最先进AI模型领域
人工智能·json·hbase·flume·storm
前网易架构师-高司机4 天前
带标注的山体滑坡塌方数据集数据集,识别率78.1%,974张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·自然灾害·山体滑坡
前网易架构师-高司机5 天前
带标注的SDD-SAR遥感船舶识别数据集,识别率92.9%,7000张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·json·数据集·遥感·sar·船舶·sdd