By Coastpear

2015-11-10 11:09:48 8 Comments

I'm trying to return a large set o json (about 2MB) but the web api returns the error:

"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property". I've tried configure the maxJsonLength property in the web.config but without success.

I've seen another answers in the stackoverflow but all related to a regular .NET MVC Controller and not an ApiController.

It's possible to bypass the internal JavaScriptSerializer maxJsonLenght or by doing some custom JsonSerializerType format?


@Coastpear 2015-11-10 14:29:37

Thanks @BMac for your help, you lead me to a solution that works.

//ResponseType it's for ApiDescription successfully generates the helper page with the right type since i'm returning a plain/text content
public async Task<HttpResponseMessage> GetLargeJsonDataWithWebAPI()
    String url = "someUrl";
    String jsonData = EnvironmentUrlHelper.GetJsonResourseAsync<YourType>(url);

    var response = Request.CreateResponse();
    response.Content = new StringContent(JsonConvert.SerializeObject(rsp));

    return response;

@BMac 2015-11-10 11:52:47

Depending on just how large the JSON payload becomes, you may have to stream the response. See this blog post. In short that blog post can be summarised by the below code:

public HttpResponseMessage PushStreamContent()
    var response = Request.CreateResponse();

    response.Content = 
    new PushStreamContent((stream, content, context) =>
        foreach (var staffMember in _staffMembers)
            var serializer = new JsonSerializer();
            using (var writer = new StreamWriter(stream))
                serializer.Serialize(writer, staffMember);

    return response;

@Coastpear 2015-11-10 14:05:20

It didn't resolve my problem but lead me to a possible resolution, using StringContent instead I can flush the content to the browser the only problem is that it not comes with the content type "applicataion/json" but with "text/plaintext"

@sjokkogutten 2015-11-10 11:42:09

Have you tried setting the MaxJsonDeserializerMembers in web.config?

      <add key="aspnet:MaxJsonDeserializerMembers" value="1000" />

@Coastpear 2015-11-10 12:30:34

Yes, it didn't work because that config it's for runtime and not for internal javascriptSerializer usage. This "internal javascriptSerializer" doesn't look for any config in the web.config. But thanks anyway. :)

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] Can I set an unlimited length for maxJsonLength in web.config?

29 Answered Questions

[SOLVED] How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

3 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] length of the string exceeds the value set on the maxJsonLength property

  • 2015-01-28 21:18:07
  • George Huang
  • 9640 View
  • 6 Score
  • 1 Answer
  • Tags:   c# json web-services

1 Answered Questions

2 Answered Questions

[SOLVED] Can't set maxJsonLength

  • 2014-03-20 13:12:29
  • Mark Highfield
  • 2502 View
  • 0 Score
  • 2 Answer
  • Tags:   c# json

Sponsored Content