I would like to use the new IAsyncEnumerable<T> in my .net core 3.1 web api. This works alright, except I am not happy with the name of the XML root element. It appears to be ArrayOfX and I would like something like Xs. How do I achieve this?
To be more specific. My controller:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
public async IAsyncEnumerable<WeatherForecast> Get()
{
await Task.Delay(0);
var rng = new Random();
for (var index = 1; index < 5; index++)
{
yield return new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
};
}
}
}
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
}
In Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMvcCore(options =>
{
options.OutputFormatters.Clear(); // Remove json for simplicity
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
}
And the XML output:
<ArrayOfWeatherForecast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><WeatherForecast><Date>2020-03-26T08:39:59.2303161+01:00</Date><TemperatureC>-13</TemperatureC><Summary>Warm</Summary></WeatherForecast><WeatherForecast><Date>2020-03-27T08:39:59.2389359+01:00</Date><TemperatureC>22</TemperatureC><Summary>Sweltering</Summary></WeatherForecast><WeatherForecast><Date>2020-03-28T08:39:59.2389696+01:00</Date><TemperatureC>33</TemperatureC><Summary>Scorching</Summary></WeatherForecast><WeatherForecast><Date>2020-03-29T08:39:59.2389719+02:00</Date><TemperatureC>-2</TemperatureC><Summary>Bracing</Summary></WeatherForecast></ArrayOfWeatherForecast>
How do I get WeatherForecasts instead of ArrayOfWeatherForecast?
