I try to send large message json(with images) using HttpClient.PostAsJsonAsync from one side and try get data in Controller using Microsoft.AspNet.Mvc version="5.2.3".

When I send small messages everything is okey.

Move code details:

private async Task<HttpResponseMessage> Method<TRequest>(string url, TRequest request,
        IEnumerable<KeyValuePair<string, string>> headers)
        using (var client = new HttpClient(_httpClientHandlerFactory.CreateHandler()))
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };
            AddHeadersToClient(client, headers);

            var response = await client.PostAsJsonAsync(url, request);

and on other side:

   public class MyController: Controller
    public ActionResult EmailBody(string siteShortName, string templateName, RequestAcceptType acceptType, [DynamicJson] dynamic model) 
        //Some logic
        return View(viewPath, model);

And when sending message I get

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

According article Can I set an unlimited length for maxJsonLength in web.config? I try to solve issue with different approches:

And in all cases I have the same issue.

1 Answers 11

Its strange to get the max lenght, but are you using base64 images? depending on quantity of images, you can easily get the max Json lenght just like that.

I can suggest a alternative to send large emails with images base64, you can create a view in html and parse with razor engine.

string view = Server.MapPath("~/Views/_YourView.cshtml");
string template = System.IO.File.ReadAllText(view);
body = Razor.Parse(template, youmodel);

Not the answer you're looking for? Browse other questions tagged or ask your own question.