I have an UmbracoApiController with a method that returns a list of products.
public class productsApiController : UmbracoApiController
{
[System.Web.Http.HttpGet]
public IEnumerable<ProductModel> getProducts()
{
List<ProductModel> result = new List<ProductModel>();
//get products code goes here ...
return result;
}
The Web API controller can automatically determine what format to return the data from the controller, usually this is will be JSON or XML. This is determined by the 'Accept' header that is sent during the request which can be set to 'application/xml' or 'application/json'.
For sure you need to add custom formatter at global asax.
public class CustomGlobal : UmbracoApplication
{
private void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
}
UmbracoApiController - output as XML (not JSON)
Hi,
I have an UmbracoApiController with a method that returns a list of products.
}
As expected, output is in JSON.
Is there a simple way to change output to XML?
Regards Thomas
Hi Thomas,
The Web API controller can automatically determine what format to return the data from the controller, usually this is will be JSON or XML. This is determined by the 'Accept' header that is sent during the request which can be set to 'application/xml' or 'application/json'.
For sure you need to add custom formatter at global asax.
More info at http://stackoverflow.com/questions/35129536/return-json-as-default-from-umbraco-api-controller
Thanks,
Alex
Hi Alex,
Thanks for your answer, but I want only this one method to return always XML.
I've ended up with this:
Regards Thomas
Hi Thomas,
Glad that you solve your issue.
You also can remove json formatter and leave only xml:
It will be the same, but more readable controllers code.
Thanks,
Alex
is working on a reply...