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

vor*_*yba 5 .net c# asp.net entity-framework asp.net-core

        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)
Run Code Online (Sandbox Code Playgroud)

我试图发送 http 请求,但在PostAsync()行上遇到异常

System.NotSupportedException:不支持“System.Action”实例的序列化和反序列化。路径:$.MoveNextAction。

Art*_*tur 5

在服务方法调用之前添加await控制器解决了我的问题


Cra*_*ert 0

因此,在不知道结构的情况下,data我只能假设以下内容。Action您有一个类,其中有 的定义。像下面这样的东西。

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

    public Action TheAction { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当您尝试序列化它时,您会收到异常。如果这就是您遇到的情况,您将需要更新表示数据的类并向其添加属性,这将排除您不想要/无法序列化的属性。像下面这样的东西

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

    public Action TheAction { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是 Newtonsoft 参考资料