aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

XML Serialization doesn't work with content negotiation

Open TheDenys opened this issue 10 years ago • 6 comments

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.

TheDenys avatar Oct 14 '16 07:10 TheDenys

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

rynowak avatar Oct 14 '16 20:10 rynowak

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.

TheDenys avatar Oct 16 '16 13:10 TheDenys

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?

rynowak avatar Oct 17 '16 15:10 rynowak

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.

TheDenys avatar Oct 18 '16 08:10 TheDenys

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.

danroth27 avatar Oct 19 '16 16:10 danroth27

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.

aspnet-hello avatar Dec 30 '17 23:12 aspnet-hello