asp.net mvc自定义JsonResult类来防止MaxJsonLength超过限制
前不久在做一个项目的时候,我用到了mvc的webapi返回了一个大数据,结果报了500错误,如下图所示:
Server Error in ‘/’ Application.
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
Stack Trace:
|
Version Information: Microsoft .NET Framework Version:2.0.50727.4952; ASP.NET Version:2.0.50727.4955
从上面可以看出错误的原因是对象在JavaScriptSerializer序列化的时候超过了默认的最大限制,所以我们需要一个类来重写JsonResult从而允许更大的数据。
- using System;
- using System.Web.Script.Serialization;
- namespace System.Web.Mvc
- {
- public class LargeJsonResult : JsonResult
- {
- const string JsonRequest_GetNotAllowed = "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
- public LargeJsonResult()
- {
- MaxJsonLength = 1024000;
- RecursionLimit = 100;
- }
- public int MaxJsonLength { get; set; }
- public int RecursionLimit { get; set; }
- public override void ExecuteResult( ControllerContext context )
- {
- if( context == null )
- {
- throw new ArgumentNullException( "context" );
- }
- if( JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
- String.Equals( context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase ) )
- {
- throw new InvalidOperationException( JsonRequest_GetNotAllowed );
- }
- HttpResponseBase response = context.HttpContext.Response;
- if( !String.IsNullOrEmpty( ContentType ) )
- {
- response.ContentType = ContentType;
- }
- else
- {
- response.ContentType = "application/json";
- }
- if( ContentEncoding != null )
- {
- response.ContentEncoding = ContentEncoding;
- }
- if( Data != null )
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = MaxJsonLength, RecursionLimit = RecursionLimit };
- response.Write( serializer.Serialize( Data ) );
- }
- }
- }
- }
你可以在Action里面使用
return new LargeJsonResult(){ Data = data } 来替代 return Json(data).
当然你也可以自己控制JavaScriptSerializer的MaxJsonLength:
return new LargeJsonResult() { Data = output, MaxJsonLength = int.MaxValue };
asp.net mvc自定义JsonResult类来防止MaxJsonLength超过限制的更多相关文章
- Asp.net MVC 自定义异常处理类
using ElegantWM.Common; using System; using System.Collections.Generic; using System.Linq; using Sys ...
- ASP.NET MVC 自定义路由中几个需要注意的小细节
本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...
- Asp.net Mvc 自定义Session (二)
在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...
- ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)
前几天Insus.NET有在数据库实现过对某一字段进行加密码与解密<使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和 ...
- ASP.NET MVC 自定义Razor视图WorkContext
概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...
- asp.net mvc 自定义pager封装与优化
asp.net mvc 自定义pager封装与优化 Intro 之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自 ...
- 一点ASP.NET MVC Html.Helper类的方法
一点ASP.NET MVC Html.Helper类 这里就只写一个Html.ActionLink()和Html.DropdownList(). Html.ActionLink()里有三个参数,第一个 ...
- Asp.net mvc 自定义全局的错误事件HandleErrorAttribute无效
Asp.net mvc 自定义全局的错误事件HandleErrorAttribute,结果无效, 原因: 1.没有在RegisterGlobalFilters 里面添加或者你要的位置添加. 2.你把这 ...
- asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值
asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...
随机推荐
- Deep Residual Learning
最近在做一个分类的任务,输入为3通道车型图片,输出要求将这些图片对车型进行分类,最后分类类别总共是30个. 开始是试用了实验室师姐的方法采用了VGGNet的模型对车型进行分类,据之前得实验结果是训练后 ...
- C#微信公众号开发系列教程三(消息体签名及加解密)
http://www.cnblogs.com/zskbll/p/4139039.html C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试) C ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- module 和 module.exports 的区别
自己理解的不好,推荐一篇文章吧: http://www.cnblogs.com/pigtail/archive/2013/01/14/2859555.html
- cookie和Session
好文推荐:http://blog.csdn.net/fangaoxin/article/details/6952954(Cookie/Session机制详解) Cookie的属性 name :cook ...
- 第20讲 HOOK和数据库编程
1,安装钩子过程可以通过SetWindowsHookEx函数来完成 2,得到当前线程ID,可以用GetCurrentThreadId 3,移除钩子可以用UnhookWindowsHookEx函数 4, ...
- linux commands
abrt-cli --since ;查看abrt捕捉的异常 alias ;别名,alias rm='rm -i':使用“ \rm ” 使用原命令 alsamixer ;图形音量调节,q 增加左声道, ...
- SqlPlus中退格键和方向键的设置
参见:http://www.cnblogs.com/wjx515/p/3717986.html http://blog.csdn.net/jacky0922/article/details/765 ...
- MSDTC事务配置
最近再用SSIS做数据归档,里面用到了分布式事务.在开发阶段是在一台计算机上运行只要是启动分布式服务就没什么问题,可是昨天把它部署到uat的时候遇到问题,错误信息是: 最后找到解决方案: 确认&quo ...
- Java后台发送邮件
一.实现思路: 1.设置连接参数 2.设置邮件相关属性 3.发送邮件 二.相关需求: 1.导入jar包: 2.设置email.properties mail.smtp.host=smtp.163.co ...