'.NET 6 web api project needs to export XML and JSON

I have a web api project that has some actions (most) that need to export JSON, and some (only a few) that need to export as XML.

Since XML isn't supported by default, i've added the

services.AddControllers(options =>
{
    options.OutputFormatters.Add(new Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter());
})

bit to my Startup.cs, and added to the Action that needs XML:

[Produces("application/xml")]
public async Task<IActionResult> ExportAsKML(Guid id)

And this works for that method, but now ALL of my controller actions default to XML. How can I keep the default to JSON, but only use XML for a certain few actions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source