/
ByXmlSerializerOutputFormatter.cs
35 lines (29 loc) · 1.21 KB
/
ByXmlSerializerOutputFormatter.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
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Net.Http.Headers;
using System.Text;
using System.Xml;
namespace Rmg.AspNetCore;
internal class ByXmlSerializerOutputFormatter : TextOutputFormatter
{
public ByXmlSerializerOutputFormatter()
{
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/xml"));
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/xml"));
SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
}
protected override bool CanWriteType(Type type)
=> typeof(IByXmlSerializer).IsAssignableFrom(type);
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
{
var bxs = (IByXmlSerializer)context.Object!;
await using var fbws = new FileBufferingWriteStream();
await using (var tw = context.WriterFactory(fbws, selectedEncoding))
{
using var xw = XmlWriter.Create(tw, new XmlWriterSettings() { Encoding = selectedEncoding, Indent = true });
bxs.Write(xw);
}
await fbws.DrainBufferAsync(context.HttpContext.Response.Body);
}
}