web.config maxJsonLength问题 [英] web.config maxJsonLength issue

查看:101
本文介绍了web.config maxJsonLength问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

移动代码详细信息:

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

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

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

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

  • added in web.config section
  • try this code in controller method
  • try to add something like this in Global.asax

在所有情况下,我都有相同的问题.

And in all cases I have the same issue.

推荐答案

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

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.

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

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);

这篇关于web.config maxJsonLength问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆