2012-03-01 104 views
28

我得到一個異常,表明JSON請求太大而無法反序列化。JsonValueProviderFactory拋出「請求太大」

它從JsonValueProviderFactory未來....

的MVC應用程序目前使用Json.Net其中有沒有問題反序列化JSON數據的自定義模型粘合劑。不過,我假設默認的JSON值提供商正在絆倒?或者內置了一些奇怪的限制?

這可能與MVC4的最新版本有關,因爲當使用MVC4的先前版本時,大量的JSON沒有問題。

那麼,有沒有辦法改變實際json值活頁夾的設置?

打算通過http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

我得到它的一些定製的東西,把它變成一本字典的印象....我無法找到與之相關的或者有任何設置,我可以改變任何代碼?

或者我可以使用另一種ValueBinder?

或其他選項?

Server Error in '/' Application. 
The JSON request was too large to be deserialized. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The JSON request was too large to be deserialized. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[InvalidOperationException: The JSON request was too large to be deserialized.] 
    System.Web.Mvc.EntryLimitedDictionary.Add(String key, Object value) +464621 
    System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +413 
    System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164 
    System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164 
    System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +373 
    System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164 
    System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +116 
    System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +34 

     System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151 
    System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +177 

回答

64

如果使用JSON.NET序列化/反序列化,你可以替換默認JsonValueProviderFactory與作爲this blog post顯示一個自定義的:

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); 
    } 
} 

,並在您Application_Start

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault()); 
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory()); 

如果你想堅持使用JavaScriptSerializer類的默認工廠,你可以調整maxJsonLength屬性在你的web.config中:

<system.web.extensions> 
    <scripting> 
     <webServices> 
      <jsonSerialization maxJsonLength="2147483644"/> 
     </webServices> 
    </scripting> 
</system.web.extensions> 
+4

和可悲的是,你只能獲得聲譽的這一微小的量,如果有人誰的答案如何分割在C#中stirng將得到桶加載:)謝謝!真的很有用......有趣的是,只是讓maxJsonLength變大就行不通了。我有一種感覺,可能在默認的Json Value綁定中存在缺陷 – 2012-03-01 20:55:24

+0

由於我的自定義模型綁定器正在處理所有事情,所以我只調用了remove。我不想成爲雙重反序列化的一切! – 2014-08-07 14:39:55

+0

如果我發送一個'string'並用JSON.NET反序列化它會怎麼樣?如果字符串太大,我會得到一個錯誤嗎? – 2015-05-07 13:05:00

61

對我來說,maxJsonLength沒有被超過。我不得不添加以下內容:

<appSettings> 
    <add key="aspnet:MaxJsonDeserializerMembers" value="150000" /> 
</appSettings> 

它在那之後工作了。

+3

你英雄.....我一直在花時間越來越無處爲序列化編寫新的處理程序,當它一直在這.....謝謝... – 2012-06-06 09:55:50

+2

我也是,這是問題。 – 2013-02-28 02:57:58

+1

很好的回答!它幫助了我!萬分感謝! – Bronek 2013-06-04 11:44:01

3

以上建議並沒有爲我工作,直到我嘗試這樣做:

// Global.asax.cs  
protected void Application_Start() { 
    MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue; 
}