• MVC中JSON字符长度超出限制的异常处理


    异常信息如下:

    使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。
     
    这个异常是在执行MVC中的JsonResult的时抛出的,根据异常的Message得知是序列化的字符串超出了maxJsonLength的限制。并得知这个属性是由JavaScriptSerializer提供的,因为MVC内置的JsonResult是用JavaScriptSerializer进行序列化的。
     
    单纯在web.config中加入下列配置节无效:
    <system.web.extensions>
           <scripting>
               <webServices>
                   <jsonSerialization maxJsonLength="20971520"/>
               </webServices>
           </scripting>
    </system.web.extensions>

    还必须重写JsonResult这个类:

    ConfigurableJsonResult 
     public class ConfigurableJsonResult : JsonResult
        {
            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("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.");
                }
    
                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();
    
                    ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;
               
                    if (section != null)
                    {
                        serializer.MaxJsonLength = section.MaxJsonLength;
                        serializer.RecursionLimit = section.RecursionLimit;
                    }
    
                    response.Write(serializer.Serialize(Data));
                }
            }
        }

    测试后可以正常使用。

    参考:

    http://www.cnblogs.com/shenba/archive/2012/02/03/2337050.html

    http://weblogs.asp.net/rashid/archive/2009/03/23/submitting-my-first-bug-after-asp-net-mvc-1-0-rtm-release.aspx

  • 相关阅读:
    关于WEB页面的强制分页打印问题
    iOS地图多个自定义大头针绘制核心代码
    PHP生成随机码
    安卓动画小结
    WKWebView遇到的问题汇总
    实现图片预加载的几种方式
    苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程
    markdown语法博客园测试
    Agreement has been updated--Edit Phone Number最便捷解决办法(不需要安全提示问题和双重认证)
    Python黑帽子:自动化内存取证
  • 原文地址:https://www.cnblogs.com/sherlock99/p/3659759.html
Copyright © 2020-2023  润新知