2014-11-06 2 views
6
내가 mvc4에서 자바 스크립트에서 데이터를 게시하는 아약스 게시물을하고있는 중이 야

발생하지만는 System.ArgumentException 예외

string exceeds the value set on the maxJsonLength property. 
Parameter name: input 
System.ArgumentException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. 

나는 이미 설정을 시도 예외 다음과 같이 실패 웹 설정에서 구성하지만

<system.web.extensions> 
    <scripting> 
     <webServices> 
      <jsonSerialization maxJsonLength="2147483647"/> 
     </webServices> 
    </scripting> 
</system.web.extensions> 

작동하지 않습니다 나는 또한 아래 링크를 시도했지만 아무것도 작동 : http://forums.asp.net/t/1751116.aspx?How+to+increase+maxJsonLength+for+JSON+POST+in+MVC3

,961,
+0

웹 구성 설정 하였다 usFarswan

+0

은 을 보았습니다. http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config?rq=1 –

답변

0

거기에 당신이 증가하는 시도 할 수 Web.config의 또 다른 설정입니다 : 난 그냥 반 시간 전에 우연히 어떤

<system.web> 
    // the default request size is 4096 KB (4 MB) this will increase it to 100 MB 
    <httpRuntime targetFramework="4.5" maxRequestLength="102400" /> 
<system.web> 
+0

이미 완료했습니다. 이 요청 길이 설정하지만 작동하지 않습니다 – usFarswan

+0

@usFarswan 일부 코드를 게시 할 수 있습니까? –

+0

기존 질문을 편집했습니다. 위의 세부 정보를 찾으십시오. – usFarswan

13

안녕 @usFarswan이가 정확히, 그리고 솔루션은 http://forums.asp.net/t/1751116.aspx?How+to+increase+maxJsonLength+for+JSON+POST+in+MVC3

입니다

그리고 이와 같이 구현하면 global.asax에 /// *****이 가리키는 다음 줄을 추가 할 수 있습니다. I 적용한

namespace MyWebApp 
{ 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 
    public class MvcApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 


      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      XmlConfigurator.Configure(); 
      DbHelper.getSessionFactory(); 

      ///// ********** 
      JsonValueProviderFactory jsonValueProviderFactory = null; 

      foreach (var factory in ValueProviderFactories.Factories) 
      { 
       if (factory is JsonValueProviderFactory) 
       { 
        jsonValueProviderFactory = factory as JsonValueProviderFactory; 
       } 
      } 

      //remove the default JsonVAlueProviderFactory 
      if (jsonValueProviderFactory != null) ValueProviderFactories.Factories.Remove(jsonValueProviderFactory); 

      //add the custom one 
      ValueProviderFactories.Factories.Add(new CustomJsonValueProviderFactory());** 
     /////************* 
     } 
    } 




    ///******** 
    public sealed class CustomJsonValueProviderFactory : ValueProviderFactory 
    { 

     private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, object value) 
     { 
      IDictionary<string, object> d = value as IDictionary<string, object>; 
      if (d != null) 
      { 
       foreach (KeyValuePair<string, object> entry in d) 
       { 
        AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value); 
       } 
       return; 
      } 

      IList l = value as IList; 
      if (l != null) 
      { 
       for (int i = 0; i < l.Count; i++) 
       { 
        AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]); 
       } 
       return; 
      } 

      // primitive 
      backingStore[prefix] = value; 
     } 

     private static object GetDeserializedObject(ControllerContext controllerContext) 
     { 
      if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) 
      { 
       // not JSON request 
       return null; 
      } 

      StreamReader reader = new StreamReader(controllerContext.HttpContext.Request.InputStream); 
      string bodyText = reader.ReadToEnd(); 
      if (String.IsNullOrEmpty(bodyText)) 
      { 
       // no JSON data 
       return null; 
      } 

      JavaScriptSerializer serializer = new JavaScriptSerializer(); 
      serializer.MaxJsonLength = int.MaxValue; //increase MaxJsonLength. This could be read in from the web.config if you prefer 
      object jsonData = serializer.DeserializeObject(bodyText); 
      return jsonData; 
     } 

     public override IValueProvider GetValueProvider(ControllerContext controllerContext) 
     { 
      if (controllerContext == null) 
      { 
       throw new ArgumentNullException("controllerContext"); 
      } 

      object jsonData = GetDeserializedObject(controllerContext); 
      if (jsonData == null) 
      { 
       return null; 
      } 

      Dictionary<string, object> backingStore = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); 
      AddToBackingStore(backingStore, String.Empty, jsonData); 
      return new DictionaryValueProvider<object>(backingStore, CultureInfo.CurrentCulture); 
     } 

     private static string MakeArrayKey(string prefix, int index) 
     { 
      return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]"; 
     } 

     private static string MakePropertyKey(string prefix, string propertyName) 
     { 
      return (String.IsNullOrEmpty(prefix)) ? propertyName : prefix + "." + propertyName; 
     } 
    } 
    ///************* 
} 
+2

저에게 도움이되는 유일한 해결책은 웹에서입니다. 감사! :) 다음은 값 제공 업체 팩토리를 대체하는보다 짧은 코드입니다. http://stackoverflow.com/a/14591946/2173353 +1 – user2173353

+0

큰 감사, 감사합니다! –

관련 문제