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   // 设置为最大值,根据需要调整
            }; 
        }
    }
}
相关推荐
Cyan_RA92 天前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
Martin-Luo2 天前
Vue3 通过json配置生成查询表单
javascript·vue.js·json
星尘库3 天前
后端json数据反序列化枚举类型不匹配的错误
json
BXCQ_xuan3 天前
软件工程实践四:MyBatis-Plus 教程(连接、分页、查询)
spring boot·mysql·json·mybatis
王维志3 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database
ID_180079054733 天前
Python采集京东店铺所有商品数据,json数据返回
json
ljh5746491193 天前
mysql 必须在逗号分隔字符串和JSON字段之间二选一,怎么选
数据库·mysql·json
小孔龙3 天前
02.Kotlin Serialization 属性序列化控制
kotlin·json
智商偏低3 天前
ASP.NET Core 中的简单授权
后端·asp.net
Cachel wood4 天前
信息检索、推荐系统模型排序质量指标:AP@K和MAP@K
windows·搜索引擎·json·推荐系统·搜索