Alen Alex
Alen Alex

Reputation: 959

Defaulting to xml serialization in asp.net core

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

Answers (2)

Edward
Edward

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

Moien Tajik
Moien Tajik

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

Related Questions