/
CsvOutputFormatter.cs
52 lines (48 loc) · 1.6 KB
/
CsvOutputFormatter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Entities.DataTransferObjects;
using Entities.Models;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;
using System.Text;
namespace WebApi.Utilities.Formatters
{
public class CsvOutputFormatter : TextOutputFormatter
{
public CsvOutputFormatter()
{
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/csv"));
SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
}
protected override bool CanWriteType(Type? type)
{
if(typeof(BookDto).IsAssignableFrom(type) ||
typeof(IEnumerable<BookDto>).IsAssignableFrom(type))
{
return base.CanWriteType(type);
}
return false;
}
private static void FormatCsv(StringBuilder buffer, BookDto book)
{
buffer.AppendLine($"{book.Id}, {book.Title}, {book.Price}");
}
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context,
Encoding selectedEncoding)
{
var response = context.HttpContext.Response;
var buffer = new StringBuilder();
if(context.Object is IEnumerable<BookDto>)
{
foreach(var book in (IEnumerable<BookDto>)context.Object)
{
FormatCsv(buffer, book);
}
}
else
{
FormatCsv(buffer, (BookDto)context.Object);
}
await response.WriteAsync(buffer.ToString());
}
}
}