解決ASP.NET MVC(post數據)Json請求太大,沒法反序列化(The JSON request was too large to be deserialized)

這個問題出現的場景並非不少,當你向服務端異步(ajax)post數據很是大的狀況下(好比作權限管理的時候給某個角色分配權限那麼就可能會出現,我所遇到的就是該角色大概200個模塊每一個模塊平均2個功能----那麼發送到服務端action的將是一個有着400個對象的數組)javascript

以前咱們向服務端異步post數組可能須要使用java

 1 $.ajax({
 2                     type: 'POST',
 3                     url: '/system/SaveRoleReModule',
 4                     dataType: "json",
 5                     contentType: "application/json;charset=utf-8",
 6                     data: JSON.stringify({ tree: treearr, roleId: roleid }),
 7                     success: function (d) {
 8                         if (d > 0) {
 9                             $.popAlter({ content: '操做成功!', hideOkBtn:true,btnTxt:'肯定'});
10                             // kq_show_info('系統提示', '操做成功', 2000);
11                         }
12                     },
13                     error: function (e) {
14                             //kq_show_info('系統提示', e.responseText, 2000);
15                     }
16                 });

可是當咱們把javascriptserializer換成json.net以後以上方式將能夠簡化爲如下寫法:web

1 $.ajax({
2 type:'post',
3 url:'',
4 data:{o:arr}
5 success:function(d){},
6 error:function(e){}
7 })

 

解決方法:ajax

方案1.asp.net mvc默認的json序列化ValueProviderFactory使用的是javascriptserializer,能夠在配置文件web.config中設置:json

<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移除,使用自定義的ValueProviderFactorymvc

 1 public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
 2     {
 3        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
 4        {
 5             if (controllerContext == null)
 6                 throw new ArgumentNullException("controllerContext");
 7             
 8             if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
 9                 return null;
10 
11             var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
12             var bodyText = reader.ReadToEnd();
13 
14             return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
15         }
16     }

global.asaxapp

 1 protected void Application_Start()
 2         {
 3             log4net.Config.XmlConfigurator.Configure();
 4             AreaRegistration.RegisterAllAreas();
 5             GlobalConfiguration.Configure(WebApiConfig.Register);
 6             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 7             RouteConfig.RegisterRoutes(RouteTable.Routes);
 8             BundleConfig.RegisterBundles(BundleTable.Bundles);
 9             ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
10             ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
11             //AutofacBuilder<ModulesRepository>.RegisterPersistent();
12         }

網上介紹最多的是第一種方案,可是我以爲json.net比默認的javascriptserializer性能要好因此採用第二種方案!asp.net

相關文章
相關標籤/搜索