.Net Core 2 Accept Header of XML returning 406

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://docs.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.