如何将 XmlDocument 传递给 Web api 中的 post 方法

How to pass XmlDocument to post method in web api

我有一个网络 api post 方法:

    [HttpPost]
    [ActionName("LoggerAttachment")]
    public string PostLoggerAttachment([FromBody] XmlDocument doc)
    {
        if (doc == null)
            return "Data is empty";
    }

如何使用 PostAsXmlAsyncXmlDocument doc 传递给此方法? 我尝试了一切,包括:

     HttpResponseMessage response = await client.PostAsXmlAsync(apiUrl, doc);

没有一个在工作。

我终于找到问题所在了。我应该使用 PostAsJsonAsync 而不是 PostAsXmlAsync。所以下面一行对我有用:

HttpResponseMessage response = await client.PostAsJsonAsync<XmlDocument>(apiUrl, doc);