亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# 如何反序列化IFormFile

C# 如何反序列化IFormFile

C#
茅侃侃 2023-07-09 15:16:45
我確信有人遇到了同樣的問題,但我沒有找到任何東西。我發送發布請求來獲取文件,并得到此模型作為響應:    public class ResponseWithFile{    public bool IsSuccessful { get; set; }    public List<int> Errors { get; set; }    public IFormFile File { get; set; }}我從控制器得到這個響應:    [Route("get")]    [HttpPost]    public async Task<IActionResult> GetFile([FromBody]GetFileDto request)    {        var result = _fileService.GetFile(request.Id, request.ContentType);        if (result.IsSuccessful)            return Ok(result);        return BadRequest(result);    }響應是正確的,我可以將其讀入字符串,但是當我嘗試將其反序列化為響應對象時,出現錯誤:            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();        string respString = string.Empty;        using (var sr = new StreamReader(resp.GetResponseStream()))        {            respString = sr.ReadToEnd();        }        var serResp = (ResponseWithFile)JsonConvert.DeserializeObject(respString);//error hereInvalidCastException:無法將“Newtonsoft.Json.Linq.JObject”類型的對象轉換為“ServiceModels.ResponseWithFile”類型我確定這是因為 IFormFile 對象。我究竟做錯了什么?
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

嘗試這個:

var serResp = JsonConvert.DeserializeObject<ResponseWithFile>(respString);

或者

  var serResp = (ResponseWithFile)JsonConvert.DeserializeObject(respString, typeof(ResponseWithFile));



查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 87 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號