1

Here's my action

public class EmployeesController : ApiController
{
    public void Post(int id, Employee employee) //Break point here ...
    {

    }
}

When I issue the following POST request, things are working perfectly.

http://localhost:64946/api/employees/12345

Host: localhost:64946
Content-Type: application/json
Content-Length: 194

{"Id":12345,"FirstName":"John","LastName":"Human"}

However, when I change the content-type to be application/xml, the employee object is null.

<Employee xmlns="http://schemas.datacontract.org/2004/07/RequestBinding.Models">
   <FirstName>John</FirstName>
   <Id>12345</Id>
   <LastName>Human</LastName>
</Employee>

Am I missing any thing?

UPDATE

I'm using Fiddler to issue request

Thanks for helping

2
  • web api by default return result in xml format .if you not pass any Content-Type in request it send result in xml format Commented Dec 21, 2013 at 2:28
  • @VijaySingh it's POST request using fiddler. When I send json in the body, it works. But when I change content-type to application/xml then replace json by xml in the body, the employee object in the action becomes null. Commented Dec 21, 2013 at 2:35

1 Answer 1

2

For this XML

<Employee xmlns="http://schemas.datacontract.org/2004/07/RequestBinding.Models">
   <FirstName>John</FirstName>
   <Id>12345</Id>
   <LastName>Human</LastName>
</Employee>

to be bound correctly, your Employee class in the Web API project must be like this. Namespace must match what is in the request, class must be public, properties must be public, property names must match the XML element names.

namespace RequestBinding.Models
{
    public class Employee
    {
        public int Id { get; set; }
        // Other properties
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

The property match. However, which namespace should much? This xmlns="http://schemas.datacontract.org/2004/07/RequestBinding.Models"?
The namespace of the model must match what is in the request. Since your XML has RequestBinding.Models, the namespace of Employee must be the same, as shown in my answer.
Confirmed! I was experiencing the same exact issue with json binding working and xml not. After setting the correct namespace it was fixed. Thanks! Another point for json being so much simpler to work with heh.
@Badri Please do you think you could help me with this question stackoverflow.com/questions/26835211/…

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.