web.config maxJsonLength问题

我尝试从一侧使用HttpClient.PostAsJsonAsync发送大消息json(带图像),并尝试使用Microsoft.AspNet.Mvc version =“5.2.3”在Controller中获取数据。

当我发送小消息时,一切都很好。

移动代码详情:

private async Task Method(string url, TRequest request, IEnumerable<KeyValuePair> headers) { using (var client = new HttpClient(_httpClientHandlerFactory.CreateHandler())) { client.DefaultRequestHeaders.Accept.Clear(); 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); ... 

另一方面:

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

当我发送消息时

使用JSON JavaScriptSerializer进行序列化或反序列化时出错。 字符串的长度超过maxJsonLength属性上设置的值。

根据文章我可以在web.config中为maxJsonLength设置无限长度吗? 我尝试用不同的方法解决问题:

  • 在web.config 部分添加
  • 在控制器方法中尝试此代码
  • 尝试在Global.asax中添加这样的东西

在所有情况下,我都有同样的问题。

获得最大长度很奇怪,但你使用的是base64图像吗? 根据图像的数量,您可以轻松获得最大的Json长度。

我可以建议使用图像base64发送大型电子邮件的替代方法,您可以在html中创建一个视图并使用razor引擎进行解析。

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