微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – web.config maxJsonLength问题

我尝试从一侧使用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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐