我找到了一些在大型响应的上下文中解决此问题的答案,但没有针对大型请求的上下文。将大文件发布到操作方法时,我收到以下异常:
使用JSON进行序列化或反序列化时出错 JavaScriptSerializer。字符串的长度超过了设置的值 在maxJsonLength属性上。
这是在我的动作方法被击中之前,所以在我的回复中提示我设置更高的json大小的帖子丰富在这里都没用。
堆栈跟踪是:
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(String input)
at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
答案 0 :(得分:5)
广泛而恐慌的谷歌搜索产生了另一个可怜的开发者这个问题,答案很简单,但令人失望的是:
我从他的答案中逐字复制了dkinchen的自定义代码,并立即起作用。
我知道这是一个链接答案,但我认为链接的文本本身就是一个非常好的答案。
答案 1 :(得分:1)
根据您当前的环境,有三种基本的处理方法:
MaxJsonLength
属性默认值。 (这仅适用于处理JSON的Web服务)MaxJsonLength
对象的JavascriptSerializer
属性以执行序列化。MVC4
来处理返回JSON
值,则可能需要覆盖默认JsonResult()
ActionResult
并手动更改最大尺寸。来源&amp;有关如何实施这三种解决方案的更多信息:http://rionscode.wordpress.com/2013/04/28/handling-larger-json-string-values-in-net-and-avoiding-exceptions/
答案 2 :(得分:0)
您可以通过在Web.config中指定键,或在Controller中指定键来解决此问题。以下是方法:
1。)在Web.config中,方法-1:
<add key="aspnet:MaxJsonDeserializerMembers" value="214748364" />
2.。)在Web.config中,方法-2:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
3.)在Controller中返回JSON数据时:
var result = Json("Data", JsonRequestBehavior.AllowGet);
result.MaxJsonLength = Int32.MaxValue;
return result;