zoukankan      html  css  js  c++  java
  • 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)

    解决方法:

    方案1.asp.net mvc默认的json序列化ValueProviderFactory使用的是javascriptserializer,可以在配置文件web.config中设置:

    <add key="aspnet:MaxJsonDeserializerMembers" value="150000000" />
    <system.web.extensions>
    <scripting>
    <webServices>
    <jsonSerialization maxJsonLength="2147483644"/>
    </webServices>
    </scripting>
    </system.web.extensions>

    方案2:重写默认的ValueProviderFactory,继承ValueProviderFactory抽象类使用json.net替换javascriptserializer,并且在application_start时将默认的ValueProviderFactory移除,使用自定义的ValueProviderFactory

    public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
        {
           public override IValueProvider GetValueProvider(ControllerContext controllerContext)
           {
                if (controllerContext == null)
                    throw new ArgumentNullException("controllerContext");
                
                if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
                    return null;
    
                var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
                var bodyText = reader.ReadToEnd();
    
                return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
            }
        }

    global.asax

    protected void Application_Start()
            {
                log4net.Config.XmlConfigurator.Configure();
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
                ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
                //AutofacBuilder<ModulesRepository>.RegisterPersistent();
            }

     建议用第一种方法,第二种方法,会遇到集合参数有时会无法接收的情况。

    源文https://www.cnblogs.com/zpc870921/p/4798640.html

  • 相关阅读:
    RAID介绍,RAID5,10制作与损坏恢复
    ELK 安装过程
    centos 系统 yum安装软件报错
    linux系统磁盘分区
    python实现遍历两个文件夹,比对文件异常,生成比对报告功能
    win10新装系统,显卡风扇转动,链接正常开机,但设备管理器如果显示,无法更新驱动
    navicat premium 破解,无限试用方法
    安装vmware workstations 12 +ubuntu 遇到的一些问题
    win10 如何打开telnet,ftp等服务
    linux集群架构-keepalived高可用
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/11198648.html
Copyright © 2011-2022 走看看