web.config maxJsonLength 问题

Ant*_*eev 5 c# asp.net asp.net-mvc json

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

当我发送小消息时,一切正常。

移动代码详情:

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);
            ...  
Run Code Online (Sandbox Code Playgroud)

另一方面:

   public class MyController: Controller
   {
    [HttpPost]
    public ActionResult EmailBody(string siteShortName, string templateName, RequestAcceptType acceptType, [DynamicJson] dynamic model) 
    {
        //Some logic
        return View(viewPath, model);
    }
...
Run Code Online (Sandbox Code Playgroud)

当我发送消息时

使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了 maxJsonLength 属性上设置的值。

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

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

小智 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);
Run Code Online (Sandbox Code Playgroud)