我的问题与this guy's one非常相似(没有答案),但区别在于我的请求是本地的,所以我对#34;大数据的替代解决方案不感兴趣&#34 ;
错误:
使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。
事实:我使用名为Rotativa的外部插件基于MVC视图创建PDF文件。它的工作原理是将视图的控制器本身作为参数传递,我需要在文档中显示的图像都在Base64中。
我不仅在web.config中设置了JsonMaxLength,还在JavasScriptSerializer中设置了JsonMaxLength。该请求是使用来自HttpClient的PostAsJsonAsync方法进行的。
Web.config
<appSettings>
<add key="aspnet:UpdatePanelMaxScriptLength" value="2147483644" />
<add key="aspnet:MaxJsonDeserializerMembers" value="2147483644" />
</appSettings>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644" />
</webServices>
</scripting>
</system.web.extensions>
代码:
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
System.Net.Http.HttpResponseMessage response = client.PostAsJsonAsync(URL, serializer.Serialize(myJsonData)).Result;
我在Stack Overflow中找到的所有其他解决方案都使用JsonResult或类似解决方案,但没有使用HttpClient。
重要的:
1-我无法使用其他插件。公司的订单。
2-我确定该对象小于Int32.MaxValue。
System.Text.Encoding.Default.GetByteCount((serializer.Serialize(myJsonData))) equals 20092960
3-也许还有另一种方式,但我是这方面的新手。
[编辑]
异常和堆栈跟踪:
[Argument Exception]使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer,String input,Type type,Int32 depthLimit)+168 System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)+211 System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)+16 System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)+69 System.Web.Mvc.ControllerBase.get_ValueProvider()+30 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor)+62 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+105 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21。&lt; BeginInvokeAction&gt; b__19(AsyncCallback asyncCallback,Object asyncState)+743 System.Web.Mvc.Async.WrappedAsyncResult
1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 System.Web.Mvc.Async.WrappedAsyncResultBase
1.Begin(AsyncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext,String actionName,AsyncCallback callback,Object state)+343 System.Web.Mvc.Controller。&lt; BeginExecuteCore&gt; b__1c(AsyncCallback asyncCallback,Object asyncState,ExecuteCoreState innerState)+25 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase
1.Begin(AsyncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback回调,对象状态)+465 System.Web.Mvc.Controller。&lt; BeginExecute&gt; b__14(AsyncCallback asyncCallback,Object callbackState,Controller controller)+18 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 System.Web.Mvc.Async.WrappedAsyncResultBase
1.Begin(AsyncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext,AsyncCallback callback,Object state)+374 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext,AsyncCallback callback,Object state)+16 System.Web.Mvc.MvcHandler。&lt; BeginProcessRequest&gt; b__4(AsyncCallback asyncCallback,Object asyncState,ProcessRequestState innerState)+52 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase
1.Begin(AsyncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+384 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态)+48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData)+16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+103 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
[更新16/11]
我能够使用JSON.NET,但我的尝试都没有成功。
还有其他想法吗? (如果有人使用JSON.NET有一些好主意,请分享。我发现的那些并不是很有用)