ASP.NET>“System.Action "的序列化和反序列化

c9x0cxw0  于 2022-12-20  发布在  .NET
关注(0)|答案(1)|浏览(194)
var json = JsonConvert.SerializeObject(data);
        var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
        var httpContent = new MultipartFormDataContent();
        httpContent.Add(stringContent, "params");

        using var httpClientHandler = new HttpClientHandler();
        httpClientHandler.ServerCertificateCustomValidationCallback =
            HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
        var httpClient = new HttpClient(httpClientHandler);

        var response = await httpClient.PostAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        if (!response.IsSuccessStatusCode)

我试图发送http请求,但在**PostAsync()**行上出现异常
System.NotSupportedException:不支持“System.Action”示例的序列化和反序列化。路径:$.移动下一个操作。

pftdvrlh

pftdvrlh1#

所以在不知道data的结构的情况下,我只能假设如下:你有一个类,里面有一个Action的定义。

public class YourData
{
    public string Name { get; set; }

    public Action TheAction { get; set; }
}

当你试图序列化它时,你会收到异常。如果这是你所遇到的,你将需要更新你的类来表示你的数据,并添加属性到它,这将排除你不想要/不能序列化的属性。

[JsonObject(MemberSerialization.OptIn)]
public class ClassWithAction
{
    [JsonProperty]
    public string Name { get; set; }

    public Action TheAction { get; set; }
}

Here is Newtonsoft Reference

相关问题