I'm trying to add a CSV Output Formatter to my .NET Core 1.1 WebApi.
I've installed WebAPIContrib.Core package (https://github.com/damienbod/WebAPIContrib.Core/tree/master/src/WebApiContrib.Core.Formatter.Csv).
My Startup now look like this:
var csvFormatterOptions = new CsvFormatterOptions();
services.AddMvcCore(options =>
{
options.OutputFormatters.Add(new CsvOutputFormatter(csvFormatterOptions));
options.FormatterMappings.SetMediaTypeMappingForFormat("csv", MediaTypeHeaderValue.Parse("text/csv"));
})
.AddJsonFormatters()
.AddJsonOptions(options => new SerializationHelper().Configure(options.SerializerSettings));
My controller method is completely generic and looks like this
[HttpGet("/api/export")]
[Produces("text/csv")]
public async Task<List<Data>> GetDataAsCsv()
{
return await _someService.GetData();
}
My issue is that if i remove the produces attribute and don't send an accept header i still get a csv. How can he know to send back a CSV ? My other methods still produce JSON which is how it should be i just don't understand why this is and i'm afraid it will cause an issue down the line.