I have added both an output formatted and an input formatted for xml for my API solution
//add formatter to support XML media type results(application/xml)
setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
//add formatter to support XML media type request(application/xml)
setupAction.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
but when I make a request using an accept header of application/xml I get a 406 has anyone else ran into this?
The content type is application/json
---- FIXED ----
If the object that the controller action returns has a constructor and the accept header is application/xml then the response will be a 406. Simply removed the constructor and then I could return XML.
re. "If the object that the controller action returns has a constructor and the accept header is application/xml then the response will be a 406." Actually, that's not correct. Correction: "If the object that the controller action returns has a constructor that takes arguments, and the object also has no 0-argument constructor, and the accept header is application/xml then the response will be a 406."
I had the same issue (.Net Core 2.2).
Because of the note on this page: https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2
I checked that my controller inherits from Controller and that my method was returning IActionResult. That was the case.
At first I added this output formatter:
setupAction.OutputFormatters.Add(new XmlSerializerOutputFormatter());
That didn't work although the formatters was in the outputformatters list and with the correct MediaType.
Then I changed to using the preferred .Net 2.2 way:
services.AddMvc(setupAction =>
{
...
})
.AddXmlSerializerFormatters();
Stil no success.
I went back to "old" way and removed AddXmlSerializerFormatters() and added
setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
I.e. Using XmlDataContractSerializerOutputFormatter instead of XmlSerializerOutputFormatter.
and then it worked.
I spend some time to figure out the differenced and my guess is that the XmlSerializerOutputFormatter can write the object type which is IEnumerable and Customer is a POCO without constructor.
This is what's in the log using XmlSerializerOutputFormatter Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector:Warning: No output formatter was found for content type 'application/xml' to write the response. Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:Warning: No output formatter was found for content type 'application/xml' to write the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With