【问题标题】:web.config maxJsonLength issueweb.config maxJsonLength 问题
【发布时间】:2016-08-11 19:13:28
【问题描述】:

我尝试使用 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?我尝试用不同的方法解决问题:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 2011-08-04
      • 2014-11-18
      相关资源
      最近更新 更多