1

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?

4
  • Check xmlbounty.Models.DocumentDto which is your input from the test method and see if you are inputting the xml. Commented Aug 14, 2020 at 9:01
  • I do not understand what you mean with "see if you are inputting the xml" Commented Aug 14, 2020 at 9:25
  • Where are you reading the xml? The issue is not parsing. You are parsing an empty xml file. Commented Aug 14, 2020 at 9:35
  • @jdweng I am not reading it since I am not sending an xml file but a string in the request body which should be decoded as xml. Commented Aug 14, 2020 at 10:01

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

That did it. Now .net reads and understands the raw request body. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.