Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#如何反序列化文件_C#_Api_Serialization_Iformfile - Fatal编程技术网

c#如何反序列化文件

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

我肯定有人也有同样的问题,但我什么也没发现。 我发送post请求以获取该文件,并将此模型作为响应:

    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));