12

I have not been able to get model binding to work when doing a POST using XML data with ASP.NET Web API. JSON data works fine.

Using a brand new Web API project, here are my model classes:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class PostResponse
{
    public string ResponseText { get; set; }
}

Here is my post method in the controller:

    public PostResponse Post([FromBody]Person aPerson)
    {
        var responseObj = new PostResponse();
        if (aPerson == null)
        {
            responseObj.ResponseText = "aPerson is null";
            return responseObj;
        }

        if (aPerson.FirstName == null)
        {
            responseObj.ResponseText = "First Name is null";
            return responseObj;
        }

        responseObj.ResponseText = string.Format("The first name is {0}", aPerson.FirstName);
        return responseObj;
    }

I am able to run it successfully with JSON from Fiddler:

Request Headers:
User-Agent: Fiddler
Host: localhost:49188
Content-Type: application/json; charset=utf-8
Content-Length: 38

Request Body:
{"FirstName":"Tom","LastName":"Jones"}

Result:
{"ResponseText":"The first name is Tom"}

When passing in XML, the Person object is not hydrated correctly:

Request Headers:
User-Agent: Fiddler
Host: localhost:49188
Content-Type: text/xml
Content-Length: 79

Request Body:
<Person>
    <FirstName>Tom</FirstName>
    <LastName>Jones</LastName>
</Person>

Result:
<ResponseText>aPerson is null</ResponseText>

From what I understand XML should work similar to JSON. Any suggestions on what I’m missing here?

Thanks,
Skip

1 Answer 1

28

Add this in your WebApiConfig.cs:

config.Formatters.XmlFormatter.UseXmlSerializer = true;

This forced Web API to use XMLSerializer instead of DataContractSerializer and allows you to pass raw XML.

Otherwise you have to pass fully qualify datacontract XML i.e.:

<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Test.WebAPI.Controllers">
<FirstName>a</FirstName>
<LastName>b</LastName>
</Person> 
Sign up to request clarification or add additional context in comments.

4 Comments

Glad you're alive, Flip! :)
For anyone still getting null models, a tip for figuring out what went wrong is simply grabbing the request content via Request.Content.ReadAsStringAsync() and trying to deserialize the XML yourself using XmlSerializer. If something is wrong, rather than just returning null like Web API does, XmlSerializer will throw an exception telling you why it can't deserialize. In my case, this is how I figured out that my XML declaration stated a UTF-16 encoding while the request itself was UTF-8 encoded. This is something I never would've figured out without just doing the deserialization myself.
+1 @Bas - almost worked for me. I had to get fancy and do this: Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin); string result = Request.Content.ReadAsStringAsync().Result;
Finally, after many answers to similar questions that tell to install MVC packages, this actually solves the problem for pure web api projects.

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.