加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – web.config maxJsonLength问题

发布时间:2020-12-15 20:59:21 所属栏目:百科 来源:网络整理
导读:我尝试从一侧使用HttpClient.PostAsJsonAsync发送大消息json(带图像),并尝试使用Microsoft.AspNet.Mvc version =“5.2.3”在Controller中获取数据. 当我发送小消息时,一切都很好. 移动代码详情: private async TaskHttpResponseMessage MethodTRequest(stri
我尝试从一侧使用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);
    }
...

当发送消息时,我得到了

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

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

>在web.config section中添加
>在控制器方法中尝试this code
>尝试在Global.asax中添加something like this

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

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读