parent
90f2ece84d
commit
9ac37fbc7a
|
|
@ -1,8 +1,8 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
|
|
@ -10,9 +10,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// <summary>
|
||||
/// Always writes a string value to the response, regardless of requested content type.
|
||||
/// </summary>
|
||||
public class TextPlainFormatter : OutputFormatter
|
||||
public class StringOutputFormatter : OutputFormatter
|
||||
{
|
||||
public TextPlainFormatter()
|
||||
public StringOutputFormatter()
|
||||
{
|
||||
SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
|
||||
SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);
|
||||
|
|
@ -39,19 +39,14 @@ namespace Microsoft.AspNet.Mvc
|
|||
public override async Task WriteResponseBodyAsync(OutputFormatterContext context)
|
||||
{
|
||||
var valueAsString = (string)context.Object;
|
||||
if (valueAsString == null)
|
||||
if (string.IsNullOrEmpty(valueAsString))
|
||||
{
|
||||
// if the value is null don't write anything.
|
||||
return;
|
||||
}
|
||||
|
||||
var response = context.ActionContext.HttpContext.Response;
|
||||
|
||||
using (var delegatingStream = new DelegatingStream(response.Body))
|
||||
using (var writer = new StreamWriter(delegatingStream, context.SelectedEncoding, 1024, leaveOpen: true))
|
||||
{
|
||||
await writer.WriteAsync(valueAsString);
|
||||
}
|
||||
await response.WriteAsync(valueAsString, context.SelectedEncoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
// Set up default output formatters.
|
||||
options.OutputFormatters.Add(new HttpNoContentOutputFormatter());
|
||||
options.OutputFormatters.Add(new TextPlainFormatter());
|
||||
options.OutputFormatters.Add(new StringOutputFormatter());
|
||||
options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||
|
||||
// Set up default input formatters.
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{
|
||||
new TextPlainFormatter(),
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter()
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{
|
||||
new TextPlainFormatter(),
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter()
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
return new List<IOutputFormatter>()
|
||||
{
|
||||
new TextPlainFormatter(),
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter()
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -490,7 +490,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
var objectResult = new ObjectResult(new Person() { Name = "John" });
|
||||
var outputFormatters = new IOutputFormatter[] {
|
||||
new HttpNoContentOutputFormatter(),
|
||||
new TextPlainFormatter(),
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter(),
|
||||
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
||||
};
|
||||
|
|
@ -531,7 +531,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
var objectResult = new ObjectResult(new Person() { Name = "John" });
|
||||
var outputFormatters = new IOutputFormatter[] {
|
||||
new HttpNoContentOutputFormatter(),
|
||||
new TextPlainFormatter(),
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter(),
|
||||
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
||||
};
|
||||
|
|
@ -566,7 +566,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
|
||||
var outputFormatters = new IOutputFormatter[] {
|
||||
new HttpNoContentOutputFormatter(),
|
||||
new TextPlainFormatter(),
|
||||
new StringOutputFormatter(),
|
||||
new JsonOutputFormatter(),
|
||||
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
||||
};
|
||||
|
|
@ -592,7 +592,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
|||
string requestAcceptCharsetHeader = "",
|
||||
bool respectBrowserAcceptHeader = false)
|
||||
{
|
||||
var formatters = new IOutputFormatter[] { new TextPlainFormatter(), new JsonOutputFormatter() };
|
||||
var formatters = new IOutputFormatter[] { new StringOutputFormatter(), new JsonOutputFormatter() };
|
||||
|
||||
return CreateMockActionContext(
|
||||
formatters,
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
public void CanWriteResult_ReturnsTrueForStringTypes(object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult)
|
||||
{
|
||||
// Arrange
|
||||
var formatter = new TextPlainFormatter();
|
||||
var formatter = new StringOutputFormatter();
|
||||
var typeToUse = useDeclaredTypeAsString ? typeof(string) : typeof(object);
|
||||
var formatterContext = new OutputFormatterContext()
|
||||
{
|
||||
|
|
@ -59,7 +59,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
var mockHttpContext = new Mock<HttpContext>();
|
||||
mockHttpContext.Setup(o => o.Response).Returns(response.Object);
|
||||
|
||||
var formatter = new TextPlainFormatter();
|
||||
var formatter = new StringOutputFormatter();
|
||||
var formatterContext = new OutputFormatterContext()
|
||||
{
|
||||
Object = null,
|
||||
|
|
@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
[InlineData("ReturnTaskOfObject_StringValue")]
|
||||
[InlineData("ReturnString")]
|
||||
[InlineData("ReturnObject_StringValue")]
|
||||
public async Task TextPlainFormatter_ForStringValues_GetsSelectedReturnsTextPlainContentType(string actionName)
|
||||
public async Task StringOutputFormatter_ForStringValues_GetsSelectedReturnsTextPlainContentType(string actionName)
|
||||
{
|
||||
// Arrange
|
||||
var server = TestServer.Create(_provider, _app);
|
||||
|
|
|
|||
|
|
@ -17,7 +17,9 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
{
|
||||
public class RazorPageTest
|
||||
{
|
||||
#pragma warning disable 1998
|
||||
private readonly RenderAsyncDelegate _nullRenderAsyncDelegate = async writer => { };
|
||||
#pragma warning restore 1998
|
||||
|
||||
[Fact]
|
||||
public async Task WritingScopesRedirectContentWrittenToViewContextWriter()
|
||||
|
|
|
|||
|
|
@ -16,7 +16,10 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
public class RazorViewTest
|
||||
{
|
||||
private const string LayoutPath = "~/Shared/_Layout.cshtml";
|
||||
|
||||
#pragma warning disable 1998
|
||||
private readonly RenderAsyncDelegate _nullRenderAsyncDelegate = async writer => { };
|
||||
#pragma warning restore 1998
|
||||
|
||||
[Fact]
|
||||
public async Task RenderAsync_AsPartial_DoesNotBufferOutput()
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
// Assert
|
||||
Assert.Equal(3, mvcOptions.OutputFormatters.Count);
|
||||
Assert.IsType<HttpNoContentOutputFormatter>(mvcOptions.OutputFormatters[0].Instance);
|
||||
Assert.IsType<TextPlainFormatter>(mvcOptions.OutputFormatters[1].Instance);
|
||||
Assert.IsType<StringOutputFormatter>(mvcOptions.OutputFormatters[1].Instance);
|
||||
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[2].Instance);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue