简体   繁体   English

在 asp.net webapi POST 中使用序列化或反序列化期间出错

[英]Error during serialization or deserialization using in asp.net webapi POST

I was trying to POST base64 string of 2 image (4MB + in Size) in a object.我试图在对象中发布 2 个图像(4MB + 大小)的 base64 字符串。 Here is the Model Sample :这是模型示例:

public class DemoModel
{
    public string No{ get; set; }
    public string OneBase64 { get; set; }
    public string TwoBase64 { get; set; }
}

Here is how I am trying to capture POST-ed data in webapi controller.这是我尝试在 webapi 控制器中捕获 POST-ed 数据的方法。

  [HttpPost]
  public string PostSimpleData([FromBody] DemoModel data)
    { 
       // do something 
    }

but when I post the json it shows serialization / deserialization error.但是当我发布 json 时,它显示序列化/反序列化错误。

在此处输入图片说明

Then I added few lines in webconfig to increase response size & json size.然后我在 webconfig 中添加了几行以增加响应大小和 json 大小。

<system.web.extensions>
    <scripting>  
         <webServices>                                                   
             <jsonSerialization maxJsonLength="2147483647" />                 
         </webServices>
    </scripting>
</system.web.extensions>

and

<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="2147483647" />
  </requestFiltering>
</security>

and

<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" />

Still no result.还是没有结果。 If I post anything except Base64 data It automatically serialize and bind with object in controllers method.如果我发布除 Base64 数据以外的任何内容,它会自动序列化并与控制器方法中的对象绑定。

here is the JSON I am trying to POST.这是我尝试发布的 JSON。

{
   "NO" : "01",
   "OneBase64" : "Some base 64 Data",
   "TwoBase64" : "Some base 64 Data"
}

any kind of help is appreciated.任何形式的帮助表示赞赏。

I changed my method from this我从这个改变了我的方法

[HttpPost]
public string PostSimpleData([FromBody] DemoModel data)
{ 
   // do something 
}

to this对此

 [HttpPost]
 public string PostSimpleData()
 {
        Stream req = Request.InputStream;
        req.Seek(0, System.IO.SeekOrigin.Begin);
        string json = new StreamReader(req).ReadToEnd();
        DemoModel model = JsonConvert.DeserializeObject<DemoModel>(json);

   //Now i Can Do whatever I want with this data
 }

and it worked like a charm.它就像一个魅力。 Thanks to this post here ASP.Net WEB API Failing POST Large JSON data from Mobile感谢这篇文章ASP.Net WEB API Failing POST Large JSON data from Mobile

You need to configure not your web api but your sender serializer instance directly by applying serializer.MaxJsonLength = Int32.MaxValue;您不需要通过应用serializer.MaxJsonLength = Int32.MaxValue;直接配置您的 web api 而是您的发送者序列化器实例serializer.MaxJsonLength = Int32.MaxValue; from where are you sending that json object你从哪里发送那个 json 对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM