2014-11-06 22 views
6
ben mvc4 içinde javascript verileri yayınlamak için ajax yazı yapıyorum

atar amaSystem.ArgumentException istisna

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. 

Zaten ayar denedim istisna aşağıdaki başarısız olur web config yapılandırmaları ama

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

çalışmıyor ben de linki aşağıda çalıştı ama hiçbir şey çalışır: http://forums.asp.net/t/1751116.aspx?How+to+increase+maxJsonLength+for+JSON+POST+in+MVC3

var editorText = eval(htmlEditor).GetHtml();  
$.ajax({type: 'POST', 
    cache: false, 
    contentType: 'application/json; charset=utf-8', 
    url: "../Home/SaveExceptionLetter", 
    data: JSON.stringify({ message: editorText }), 
    datatype: 'json', 
    success: function() { 
    }); 
} }); 

[HttpPost] 
[ValidateInput(false)] 
public void SaveExceptionLetter(string message){ 
    //processing this message 
} 

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. 
Parameter name: input 
    at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) 
    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) 
+0

Web yapılandırma ayarı yapıldı usFarswan

+0

http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config?rq=1 –

cevap

0

orada artırmak için deneyebileceğiniz web.config'de başka ayardır: Ben sadece yarım saat önce geldi neyi

<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

'a bir göz attım Bu istek uzunluk ayarı Ama çalışmıyor – usFarswan

+0

@usFarswan bir kod gönderebilir misiniz? –

+0

Mevcut soruyu düzenledim, lütfen – usFarswan

13

Merhaba @usFarswan bu tam olduğunu ve çözüm http://forums.asp.net/t/1751116.aspx?How+to+increase+maxJsonLength+for+JSON+POST+in+MVC3

olduğunu

Ve bunu şu şekilde uygulayabilirsiniz: global.asax /// ***** ile işaretlenmiş aşağıdaki satırları ekleyin. Ben uyguladık

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

'un yukarısındaki detayları bulun. Bu, benim için çalışan web üzerindeki tek çözüm. Teşekkürler! :) Burada değer sağlayıcı fabrikasını değiştirmek için daha kısa bir kod: http://stackoverflow.com/a/14591946/2173353 +1 – user2173353

+0

Harika bir ruh hali, Teşekkür ederim! –