Reputation: 959
I'm trying to create a Web-Api based on asp.net core. My requirement is that, it should support xml
serialization by default and not json
.
I added
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddXmlSerializerFormatters();
But still, the default serializer is json.
I need it to return xml
without the client having to specify
{"Accept":"application/xml"}
Upvotes: 3
Views: 4999
Reputation: 29986
For changing default serializer, you could try
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => {
options.OutputFormatters.Insert(0, new XmlDataContractSerializerOutputFormatter());
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Upvotes: 0
Reputation: 2311
As said here, you must force your application to produce XML instead of JSON:
services.AddMvc(opt =>
{
opt.Filters.Add(new ProducesAttribute("application/xml"));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddXmlSerializerFormatters();
Upvotes: 3