JSON 请求太大,无法反序列化
方案一
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>
方案二
重写默认的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(); }
网上介绍最多的是第一种方案,但是我觉得json.net比默认的javascriptserializer性能要好所以采用第二种方案!
本文来自博客园,作者:jevan,转载请注明原文链接:https://www.cnblogs.com/DoNetCShap/p/18469069