fixed rss xml encoding always being utf-16

This commit is contained in:
Mia Rose Winter 2024-02-04 13:14:32 +01:00
parent 4e521145dd
commit 38e58f7b71
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -13,6 +13,7 @@ public class SyndicationFeedFormatter : TextOutputFormatter {
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/atom+xml")); SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/atom+xml"));
SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
} }
protected override bool CanWriteType(Type? type) protected override bool CanWriteType(Type? type)
@ -24,9 +25,9 @@ protected override bool CanWriteType(Type? type)
var feed = context.Object as SyndicationFeed; var feed = context.Object as SyndicationFeed;
await using var stringWriter = new StringWriter(); await using var stream = new MemoryStream();
await using var rssWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { await using var rssWriter = XmlWriter.Create(stream, new XmlWriterSettings {
Async = true Async = true, Encoding = selectedEncoding
}); });
System.ServiceModel.Syndication.SyndicationFeedFormatter formatter; System.ServiceModel.Syndication.SyndicationFeedFormatter formatter;
if (context.ContentType.Value?.StartsWith("application/rss+xml") is true) { if (context.ContentType.Value?.StartsWith("application/rss+xml") is true) {
@ -41,6 +42,6 @@ protected override bool CanWriteType(Type? type)
formatter.WriteTo(rssWriter); formatter.WriteTo(rssWriter);
rssWriter.Close(); rssWriter.Close();
await httpContext.Response.WriteAsync(stringWriter.ToString(), selectedEncoding); await httpContext.Response.BodyWriter.WriteAsync(stream.ToArray());
} }
} }