c#如何反序列化文件
我肯定有人也有同样的问题,但我什么也没发现。 我发送post请求以获取该文件,并将此模型作为响应:c#如何反序列化文件,c#,api,serialization,iformfile,C#,Api,Serialization,Iformfile,我肯定有人也有同样的问题,但我什么也没发现。 我发送post请求以获取该文件,并将此模型作为响应: public class ResponseWithFile { public bool IsSuccessful { get; set; } public List<int> Errors { get; set; } public IFormFile File { get; set; } } InvalidCastException:无法将“Newto
public class ResponseWithFile
{
public bool IsSuccessful { get; set; }
public List<int> Errors { get; set; }
public IFormFile File { get; set; }
}
InvalidCastException:无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“ServiceModels.ResponseWithFile”类型
我肯定是因为文件对象。我做错了什么?试试这个:
var serResp = JsonConvert.DeserializeObject<ResponseWithFile>(respString);
没有人会阅读异常消息:(难道不是
ifformfile
用于表示上载到web服务的文件,即在使用
时?响应文件建议您在下载文件时尝试将其用作响应。向我们显示respString
JSON字符串,以便我们可以看到您正在接收的数据。现在我得到了JsonSerializationException:无法创建Microsoft.AspNetCore.Http.ifformfile类型的实例。类型是接口或抽象类,无法实例化。看起来我不应该使用ifformfile来接收files@Jamil我相信,你会得到的answer@Jamil看起来您应该开始阅读异常消息了。“无法创建Microsoft.AspNetCore.Http.ifformfile类型的实例。类型是接口或抽象类,无法实例化。”“意味着ifformfile
是一个接口,无法实例化。没有人可以实例化接口的实例。问题是我没有正确地从流中读取,我可能不应该在那里使用ifformfile。我接受这个答案,因为从技术上讲,这是我问题的解决方案(尽管问题在另一个地方)。
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 here
var serResp = JsonConvert.DeserializeObject<ResponseWithFile>(respString);
var serResp = (ResponseWithFile)JsonConvert.DeserializeObject(respString, typeof(ResponseWithFile));