我已经找到了几个答案,在一个大的答复范围内解决这个问题,但没有一个对我的背景下,一个大的要求。当我将一个大文件投递到操作方法时,我会得到以下异常:
使用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)发布于 2014-12-12 14:08:03
广泛而惊慌失措的谷歌搜索产生了另一个问题,这个问题的答案很简单,但却是令人失望的侵入性:
首先,您必须构建一个自定义JsonValueProviderFactory。
我从他的回答中逐字抄袭了dkinchen的自定义代码,它立即起作用了。
我知道这是一个链接答案,但链接的文本本身是一个相当好的答案,我认为。
发布于 2014-12-12 12:01:03
根据当前环境的不同,有三种处理此问题的基本方法:
MaxJsonLength中设置web.config属性默认值。(这只适用于处理JSON的web服务)MaxJsonLength对象的JavascriptSerializer属性以执行序列化。MVC4来处理返回JSON值的操作,则可能需要重写默认的JsonResult() ActionResult并手动更改最大大小。源&关于如何实现这三个解决方案的更多信息:http://rionscode.wordpress.com/2013/04/28/handling-larger-json-string-values-in-net-and-avoiding-exceptions/
发布于 2014-12-12 14:10:21
您可以通过在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;https://stackoverflow.com/questions/27443125
复制相似问题