How can I have my vnext API to return XML and JSON ?
I thought that using content-type with application/xml would work as it was before. Note that I tryed with Accep
Faced the similar problems, Have to process in one WEB REST API service ,with using ASP .NET MVC Core 1.1.0 , two types of XML body requests DataContractSerializer and XmlSerializer.
So, in my case I need FromXmlBody and XmlResult with a parameter of the type of the XML serialization. Read this thread and think to write a peace of the code with work around, but when I have a look over the GitHub. I found that the solution has already existed. I have checked, it looks like a quality software engineering solution. I want to share the links: Here is new XML extension for Asp.NET Core ver. >=1.1.0 XmlResult and FromXmlBody Extensions of ASP.NET Core MVC formatters for XML input and output using DataContractSerializer and XmlSerializer. https://github.com/Wallsmedia/XmlResult nugget package: https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions It works fine for both type of Xml serialize types(DataContractSerializer and XmlSerializer) in one service/controller/method. Here is the example: [Route("api/[controller]")]
public class XmlExtController : Controller
{
// GET api/[controller]/xml
[HttpGet("xml")]
public ActionResult GetXmlObject()
{
object obj = new PurchaseOrder();
return new XmlResult(obj);
}
// GET api/[controller]/dcxml
[HttpGet("dcxml")]
public ActionResult GetDcXmlObject()
{
object obj = new PurchaseOrder();
return new XmlResult(obj) { XmlSerializerType = XmlSerializerType.DataContractSerializer };
}
// POST api/[controller]/xml
[HttpPost("xml")]
public void PostXml([FromXmlBody]PurchaseOrder value)
{
var x = value;
x.billTo.street += " 123";
}
// POST api/[controller]/dcxml
[HttpPost("dcxml")]
public void PostDcXml([FromXmlBody(XmlSerializerType = XmlSerializerType.DataContractSerializer)]PurchaseOrder value)
{
var x = value;
x.billTo.street += "No -10";
}
}