Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi force action to return xml

I have this action:

public IHttpActionResult SearchFor(int aboItemType, DTO.FilterColumns filter)
{
    //Do stuff...
    return Ok<DataSet>(ds);
}

My client does:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

var response = client.PostAsJsonAsync(myurl).Result;
if (response.IsSuccessStatusCode)
{
    var results = HttpUtility.HtmlDecode(response.Content.ReadAsStringAsync().Result);
}

The above scenario works perfectly. However, if I comment the Accept line, the action returns the dataset in json format.

I would like to force this one particular action to always send the result in xml. Is this possible? Maybe with an attribute?

like image 369
Ivan-Mark Debono Avatar asked Mar 27 '14 08:03

Ivan-Mark Debono


1 Answers

I used Сonfiguration.Formatters.XmlFormatter

public IHttpActionResult Get()
{
 ...
  return Content(HttpStatusCode.OK, Model, Configuration.Formatters.XmlFormatter);
}
like image 175
Ilya Avatar answered Nov 02 '22 18:11

Ilya