我尝试从一侧使用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属性上设置的值。
根据文章"我可以在web.config中设置maxJsonLength的无限长度吗?"?我试图用不同的方法来解决问题:
- 已在web.config节中添加
- 在控制器方法中尝试此代码
- 尝试在Global.asax中添加类似的内容
在所有情况下,我都有同样的问题。
获得最大长度很奇怪,但您使用的是base64图像吗?根据图像的数量,你可以很容易地得到最大Json长度。
我可以建议一种替代方案,用图片base64发送大型电子邮件,你可以用html创建一个视图,并用剃刀引擎进行解析。
string view = Server.MapPath("~/Views/_YourView.cshtml");
string template = System.IO.File.ReadAllText(view);
body = Razor.Parse(template, youmodel);