Web API XML Post Model is Null

I am sending XML to Web API method using the POST method. The Web API hosted using OWIN middleware. When JSON send to the request it works fine but for XML the model is always null.

I already have a setup GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

How are you sending the XML? Are you telling the server that the Content-Type you are sending is XML (taking advantage of the content negotiation capabilities)?

Examples of the Content-Type header:
Content-Type: application/json; charset=utf-8
Content-Type: text/xml
Contant-Type: application/xml

If using jQuery, you can use the "data" parameter to set the content type:
    type: method,
    url: url,
    dataType: "xml",
    data: data || null,
    success: function (data) {
        // omitted

Fiddler: It's a good idea to use Fiddler to experiment with your Web API as well. Quick and easy.
JRR75Author Commented:
I am testing using Fiddler to test my Web  API and using Content-Type: application/xml.
Are you checking for Model binding errors? stick this at the beginning of your controller methods:
if (!ModelState.IsValid)
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);

Open in new window

Deserialise the stream yourself
In your controller, can you get the raw content of the request and try to deserialise it yourself? This will also help. (this is just for troubleshooting and should not be how you deal with the deserialisation)
    string data = Request.Content.ReadAsStringAsync() ;
    new XmlSerializer(typeof(yourType)).Deserialize(new StringReader(data));
} catch(Exception ex)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

JRR75Author Commented:
Thanks for the guidance and it was an issue in Model binding and expecting XML namespace information. Yes I am able to fix the issue,but it's using DataContractSeralizer instead XmlSerializer.

I have already configured GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; in Startup.cs.
If this is set GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true, then I'm afraid i cannot think of any other reasons why there should still be problems. Is there a specific error message? I cannot think of anything else though...
JRR75Author Commented:
No error message. If xmlseralizer utilized it should not expect namespace right? but it expects other wise i get namespace required message.
JRR75Author Commented:
If i use config.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;, i get Unsupported Media Type error message.
What is this method doing? Is it a multi-part form post? Are there files in the body? Maybe post some code snippets as well. Client side and server side
PS: I think this error is actually a step in the right direction.
JRR75Author Commented:
Thank you. It was a problem with model mapping. on API side few request type properties are XML attributes  and when it's mapped with other class type using automapper, it looks unable to  map it correctly. Now i have made all  request type properties as XML elements and mapped it, now no media type error and works fine.
JRR75Author Commented:
The instruction helped me to troubleshoot the issue.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.