web.config maxJsonLength问题

时间:2016-04-19 14:24:16

标签: c# asp.net json asp.net-mvc

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

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

移动代码详情:

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.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属性上设置的值。

根据文章Can I set an unlimited length for maxJsonLength in web.config?,我尝试用不同的方法解决问题:

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

1 个答案:

答案 0 :(得分:0)

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

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

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