XML Serialization doesn't work with content negotiation
Configured MVC as following:
services
.AddMvc(config =>
{
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
})
.AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
Send these headers from client:
Content-Type: application/xml Accept: application/xml
The result from server is coming in JSON format anyway.
For the endpoint you are hitting, is the data type you are trying to serialize XmlSerializable? The xml formatters will not attempt to serialize types they don't understand
Ryan, could you please be more specific?
It worked with simple anonymous types in previous version and now it doesn't.
So in controller i could return
Ok(new {FieldA = "a", FieldB = "b"})
and get the result in JSON and XML depending on Accept header. With latest dotnet installed and SDK it's always JSON.
Used samples from here.
I tried this out, and the following exception is handled by the XmlSerializerOutputFormatter
Exception thrown: 'System.InvalidOperationException' in System.Xml.dll
Additional information: <>f__AnonymousType0`1[System.Int32] cannot be serialized because it does not have a parameterless constructor.
When you say that this working in previous version what do you mean exactly?
Sorry for delay, By previous version i mean previous release of dotnet core runtime and core SDK. Now i have the latest from Dotnet Downloads section and i don't remember what exact versions i had before update.
I'm not sure how this could have worked previously. If you think this is a regression it would be great if you could track down which specific version this used to work with.
This issue is being closed because it has not been updated in 3 months.
We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.