31

Intro:

Web application, ASP.NET MVC 3, a controller action that accepts an instance of POCO model class with (potentially) large field.

Model class:

public class View
{
    [Required]
    [RegularExpression(...)]
    public object name { get; set; }
    public object details { get; set; }
    public object content { get; set; } // the problem field
}

Controller action:

[ActionName(...)]
[Authorize(...)]
[HttpPost]
public ActionResult CreateView(View view)
{
    if (!ModelState.IsValid) { return /*some ActionResult here*/;}
    ... //do other stuff, create object in db etc. return valid result
}

Problem:

An action should be able to accept large JSON objects (at least up to hundred megabytes in a single request and that's no joke). By default I met with several restrictions like httpRuntime maxRequestLength etc. - all solved except MaxJsonLengh - meaning that default ValueProviderFactory for JSON is not capable of handling such objects.

Tried:

Setting

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647"/>
      </webServices>
    </scripting>
  </system.web.extensions>
  • does not help.

Creating my own custom ValueProviderFactory as described in @Darin's answer here:

JsonValueProviderFactory throws "request too large"

  • also failed because I have no possibility to use JSON.Net (due to non-technical reasons). I tried to implement correct deserialization here myself but apparently it's a bit above my knowledge (yet). I was able to deserialize my JSON string to Dictionary<String,Object> here, but that's not what I want - I want to deserialize it to my lovely POCO objects and use them as input parameters for actions.

So, the questions:

  1. Anyone knows better way to overcome the problem without implementing universal custom ValueProviderFactory?
  2. Is there a possibility to specify for what specific controller and action I want to use my custom ValueProviderFactory? If I know the action beforehand than I will be able to deserialize JSON to POCO without much coding in ValueProviderFactory...
  3. I'm also thinking about implementing a custom ActionFilter for that specific problem, but I think it's a bit ugly.

Anyone can suggest a good solution?

Community
  • 1
  • 1
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68

3 Answers3

67

The built-in JsonValueProviderFactory ignores the <jsonSerialization maxJsonLength="50000000"/> setting. So you could write a custom factory by using the built-in implementation:

public sealed class MyJsonValueProviderFactory : 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 = 2147483647;
        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;
    }
}

The only modification I did compared to the default factory is adding the following line:

serializer.MaxJsonLength = 2147483647;

Unfortunately this factory is not extensible at all, sealed stuff so I had to recreate it.

and in your Application_Start:

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<System.Web.Mvc.JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new MyJsonValueProviderFactory());
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    There are a lot of people tasking about this topic and this is the only solution I could find that worked in my MVC4 Application. Thank you! – stitz Feb 23 '13 at 11:41
  • 2
    Super! Works great for Binding large Json object. For GET requests with large Json object I am using the class over here: http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/ – Gautam Jain Mar 20 '13 at 10:28
  • 3
    If you're having trouble posting large Json structures over an ajax post to MVC4 Controller, try this before any other stuff. Tried a lot of other aproaches with no luck and this alone saved my week. Thanks a lot @DarinDimitrov ! – Marcelo Myara Feb 17 '14 at 13:04
  • Brilliant solution - works with MVC3 too - adding the settings to the web config wasn't working, so thank you very much. – Adam Diament Apr 02 '14 at 17:05
  • 1
    works for MVC5 also using .Net 4.5. excellent, spent way to many hours trying to resolve the maxLength issue. this also does *not* negatively affect JSON.net base controller. – JQII Sep 08 '14 at 19:33
  • Does this work for Web Forms as well? Where should this class be located? – user1012598 Feb 10 '15 at 06:00
  • 1
    4 years later: Yeah!! That is still the best (only) solution to find, regarding this error... Thanks @Darin – Joshit May 16 '17 at 14:32
18

I found that the maxRequestLength did not solve the problem however. I resolved my issue with the below setting. It is cleaner than having to implement a custom ValueProviderFactory

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

Credit goes to the following questions:

JsonValueProviderFactory throws "request too large"

Getting "The JSON request was too large to be deserialized"

This setting obviously relates to a highly complex json model and not the actual size.

Community
  • 1
  • 1
Oliver
  • 35,233
  • 12
  • 66
  • 78
  • 2
    This might be useful for someone, although my original issue was not affected by this setting. You had a really complex JSON document with a lot of elements - so that setting helped you - and I had quite simple document with large encoded content for some of the values. – Sergey Kudriavtsev Aug 23 '13 at 22:00
  • Oliver - You hit the max # of items in the json dictionary rather than content length or complexity. There's a limitation of 1000 items by default in the JavaScriptSerializer. Your answer is correct for this scenario but here's the link on the subject https://msdn.microsoft.com/en-us/library/hh975440.aspx – Tristan Warner-Smith Feb 02 '15 at 11:48
3

The solution of Darin Dimitrov works for me but i need reset the position of the stream of the request before read it, adding this line:

controllerContext.HttpContext.Request.InputStream.Position = 0;

So now, the method GetDeserializedObject looks like this:

 private static object GetDeserializedObject(ControllerContext controllerContext)
    {
        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
        {
            // not JSON request
            return null;
        }
        controllerContext.HttpContext.Request.InputStream.Position = 0;
        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 = 2147483647;
        object jsonData = serializer.DeserializeObject(bodyText);
        return jsonData;
    }