Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 2 Accept Header of XML returning 406

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.

like image 776
Hizzy Avatar asked Nov 27 '17 18:11

Hizzy


2 Answers

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."

like image 85
Ken Avatar answered Nov 18 '22 08:11

Ken


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.

like image 45
P Schalkwijk Avatar answered Nov 18 '22 06:11

P Schalkwijk