I have an ASP.Net Web API project which already contains controllers to return result in JSON format. Now I have to add new controllers which should receive and return only XML. I know that I can use the following option to push controllers to return XML serialized objects:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true
But, if I do it, I expect that the old controllers will also return data in XML format, right? This not what I need. So, how can I achieve my goal and return XML serialized objects only from some of controllers? Thank you.
UPDATE
I have tried suggested approach, but it does not work for me. I am testing using Fiddler. Please pay attention that result is still JSON (the last screenshot) Here is additional information:
Controller:
Request class:
Response class:
Request in Fiddler:
Response in Fiddler:
Content negotiation or serialization is not something that controllers should be concerned about.
Out of the box ASP.NET Web API can return both XML and JSON content, the client may request certain format by setting Accept HTTP header.
Set it to:
Accept: application/json
if you want to get JSON back
Accept: application/xml
if you want to get XML back
EDIT: Also note that Web API by default uses DataContractSerializer, answering questions in your comments:
[DataContract(Namespace = "schemas.datacontract.org/2004/07/Test.Models" )]
public class TheThing
{
[DataMember]
public string Name { get; set; }
[DataMember(Name = "contentname")]
public string ContentName { get; set; }
}