[Fixes #2541] Use custom stream writer for Xml output formatters
This commit is contained in:
parent
22f1881cc6
commit
6ef8be92ff
|
|
@ -158,7 +158,9 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream,
|
public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream,
|
||||||
[NotNull] XmlWriterSettings xmlWriterSettings)
|
[NotNull] XmlWriterSettings xmlWriterSettings)
|
||||||
{
|
{
|
||||||
return XmlWriter.Create(writeStream, xmlWriterSettings);
|
return XmlWriter.Create(
|
||||||
|
new HttpResponseStreamWriter(writeStream, xmlWriterSettings.Encoding),
|
||||||
|
xmlWriterSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
@ -167,10 +169,7 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
var tempWriterSettings = WriterSettings.Clone();
|
var tempWriterSettings = WriterSettings.Clone();
|
||||||
tempWriterSettings.Encoding = context.SelectedEncoding;
|
tempWriterSettings.Encoding = context.SelectedEncoding;
|
||||||
|
|
||||||
var innerStream = context.HttpContext.Response.Body;
|
using (var xmlWriter = CreateXmlWriter(context.HttpContext.Response.Body, tempWriterSettings))
|
||||||
|
|
||||||
using (var outputStream = new NonDisposableStream(innerStream))
|
|
||||||
using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings))
|
|
||||||
{
|
{
|
||||||
var obj = context.Object;
|
var obj = context.Object;
|
||||||
var runtimeType = obj?.GetType();
|
var runtimeType = obj?.GetType();
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,9 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream,
|
public virtual XmlWriter CreateXmlWriter([NotNull] Stream writeStream,
|
||||||
[NotNull] XmlWriterSettings xmlWriterSettings)
|
[NotNull] XmlWriterSettings xmlWriterSettings)
|
||||||
{
|
{
|
||||||
return XmlWriter.Create(writeStream, xmlWriterSettings);
|
return XmlWriter.Create(
|
||||||
|
new HttpResponseStreamWriter(writeStream, xmlWriterSettings.Encoding),
|
||||||
|
xmlWriterSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
@ -144,10 +146,7 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
var tempWriterSettings = WriterSettings.Clone();
|
var tempWriterSettings = WriterSettings.Clone();
|
||||||
tempWriterSettings.Encoding = context.SelectedEncoding;
|
tempWriterSettings.Encoding = context.SelectedEncoding;
|
||||||
|
|
||||||
var innerStream = context.HttpContext.Response.Body;
|
using (var xmlWriter = CreateXmlWriter(context.HttpContext.Response.Body, tempWriterSettings))
|
||||||
|
|
||||||
using (var outputStream = new NonDisposableStream(innerStream))
|
|
||||||
using (var xmlWriter = CreateXmlWriter(outputStream, tempWriterSettings))
|
|
||||||
{
|
{
|
||||||
var obj = context.Object;
|
var obj = context.Object;
|
||||||
var runtimeType = obj?.GetType();
|
var runtimeType = obj?.GetType();
|
||||||
|
|
|
||||||
|
|
@ -272,7 +272,9 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
var body = outputFormatterContext.HttpContext.Response.Body;
|
var body = outputFormatterContext.HttpContext.Response.Body;
|
||||||
body.Position = 0;
|
body.Position = 0;
|
||||||
|
|
||||||
var content = new StreamReader(body).ReadToEnd();
|
var content = new StreamReader(
|
||||||
|
body,
|
||||||
|
new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd();
|
||||||
XmlAssert.Equal(expectedOutput, content);
|
XmlAssert.Equal(expectedOutput, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -227,8 +227,9 @@ namespace Microsoft.AspNet.Mvc.Xml
|
||||||
// Assert
|
// Assert
|
||||||
var body = outputFormatterContext.HttpContext.Response.Body;
|
var body = outputFormatterContext.HttpContext.Response.Body;
|
||||||
body.Position = 0;
|
body.Position = 0;
|
||||||
|
var content = new StreamReader(
|
||||||
var content = new StreamReader(body).ReadToEnd();
|
body,
|
||||||
|
new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd();
|
||||||
XmlAssert.Equal(expectedOutput, content);
|
XmlAssert.Equal(expectedOutput, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ContentNegotiationWebSite.Models;
|
using ContentNegotiationWebSite.Models;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
using Microsoft.AspNet.Mvc.Internal;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
@ -39,11 +40,7 @@ namespace ContentNegotiationWebSite
|
||||||
builder.AppendLine();
|
builder.AppendLine();
|
||||||
builder.AppendLine("END:VCARD");
|
builder.AppendLine("END:VCARD");
|
||||||
|
|
||||||
var responseStream = new NonDisposableStream(context.HttpContext.Response.Body);
|
await context.HttpContext.Response.WriteAsync(builder.ToString(), context.SelectedEncoding);
|
||||||
using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024))
|
|
||||||
{
|
|
||||||
await writer.WriteAsync(builder.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ContentNegotiationWebSite.Models;
|
using ContentNegotiationWebSite.Models;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
using Microsoft.AspNet.Mvc.Internal;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
@ -42,11 +43,7 @@ namespace ContentNegotiationWebSite
|
||||||
builder.AppendLine();
|
builder.AppendLine();
|
||||||
builder.AppendLine("END:VCARD");
|
builder.AppendLine("END:VCARD");
|
||||||
|
|
||||||
var responseStream = new NonDisposableStream(context.HttpContext.Response.Body);
|
await context.HttpContext.Response.WriteAsync(builder.ToString(), context.SelectedEncoding);
|
||||||
using (var writer = new StreamWriter(responseStream, context.SelectedEncoding, bufferSize: 1024))
|
|
||||||
{
|
|
||||||
await writer.WriteAsync(builder.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue