How to configure the actions within a Web API Controller, to return XML rather than JSON?
I have an action which returns a UserProfile object which has XmlElement attributes:
[HttpGet]
public UserProfile SearchByEmail(string siteName, string email)
{
var userProfile = this._profileFinderByEmail.Find(siteName, email);
if (userProfile == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return userProfile;
}
When when I run this action, it returns application/json rather than xml. How could I return xml?
Judging by Fiddler the request header that I was sending had the below Accept header key:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
that I expected it to return xml but it doesn't.
How to fix it?
I've also set the below value in the WebApiConfig class:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
Viewing from Chrome/Network tab, I can see the below error:
Status Code:406 Not Acceptable