vchan
vchan

Reputation: 860

POST xml to .Net Core Web API

I have this action on a .Net Core Web API Controller called HomeController:

[HttpPost]
public IActionResult TestMethod([FromForm] DocumentDto xml) {
    int abc = 0;

    return Ok();
}

and the model DocumentDto:

[XmlRoot(ElementName = "document", Namespace = "")]
public class DocumentDto
{
    [XmlElement(DataType = "string", ElementName = "id")]
    public string Id { get; set; }

    [XmlElement(DataType = "string", ElementName = "content")]
    public string Content { get; set; }

    [XmlElement(DataType = "string", ElementName = "author")]
    public string Author { get; set; }
}

I have also added a breakpoint on the int abc = 0; and I use Postman to hit the action as displayed below

Request Headers

Request Body

The xml used for the request is the following

<document>
    <id>12345</id>
    <content>This is a Test</content>
    <author>vchan</author>
</document>

However, during debugging the xml variable has null properties as shown below

Debugging

Also, on the Startup.cs file I have included the AddXmlSerializerFormatters() and it is as below:

services
    .AddMvc()
    .AddXmlSerializerFormatters()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

Why is the xml not parsed?

Upvotes: 1

Views: 1612

Answers (1)

itdoesntwork
itdoesntwork

Reputation: 1871

Based on your request the raw XML should be parsed [FromBody] and not [FromForm].

Upvotes: 2

Related Questions