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
Here is the updated answer for MVC6 rc1
Startup.cs (using MvcXmlMvcBuilderExtensions)
public void ConfigureServices(IServiceCollection services)
{
var mvcBuilder = services.AddMvc();
mvcBuilder.AddXmlSerializerFormatters();
// or mvcBuilder.AddXmlDataContractSerializerFormatters()
project.json
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final",
Don't forget as ASP.NET 5 was renamed to ASP.NET Core 1.0 and so Microsoft.AspNet.Mvc
became Microsoft.AspNetCore.Mvc i.e.
"dependencies": {
"Microsoft.AspNetCore.Mvc" : "1.0.2",
"Microsoft.AspNetCore.Mvc.Formatters.Xml" : "1.0.2",
Updated details following release of .Net Core 1.0.0
startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(config =>
{
// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
project.json
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.Formatters.Xml": "1.0.0",
For more help see Shawn Wildermuths blog post on the subject: Content Negotiation in ASP.NET Core
Updated answer for ASP.NET Core 1.1:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(config => {
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
}
Csproj:
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml" Version="1.1.3" />
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";
}
}
You Request hastto send the AcceptHeader application/xml
Accept: Which media types are acceptable for the response, such as “application/json,” “application/xml,” or a custom media type such as "application/vnd.example+xml
content-type defines what you are sending see also Difference between Accept and ContentType Header
I'm not sure wether the content negiotiation is for xml is activated per default in asp.net 5 webapi
have lookt at this article: Content Negotiation and Web API for the ASP.NET MVC Developer
By default Xml formatters are not included as part of the Microsoft.AspNet.Mvc
package. You need to reference another package called Microsoft.AspNet.Mvc.Xml
for this.
Example on how you can add the formatters:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.ConfigureMvc(options =>
{
// This adds both Input and Output formatters based on DataContractSerializer
options.AddXmlDataContractSerializerFormatter();
// To add XmlSerializer based Input and Output formatters.
options.InputFormatters.Add(new XmlSerializerInputFormatter());
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});