Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit b51f0f3

Browse files
committed
Adding TextPlainFormatter to always handle returning strings as text\plain format.
1 parent fd201f9 commit b51f0f3

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNet.Http;
9+
using Microsoft.AspNet.Mvc;
10+
using Microsoft.AspNet.Mvc.HeaderValueAbstractions;
11+
12+
namespace Microsoft.AspNet.Mvc
13+
{
14+
/// <summary>
15+
/// Writes a string value to the response.
16+
/// </summary>
17+
public class TextPlainFormatter : OutputFormatter
18+
{
19+
public TextPlainFormatter()
20+
{
21+
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/plain"));
22+
SupportedEncodings.Add(Encoding.GetEncoding("utf-8"));
23+
}
24+
25+
public override bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
26+
{
27+
if (base.CanWriteResult(context, contentType))
28+
{
29+
var valueAsString = context.Object as string;
30+
if (valueAsString != null)
31+
{
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
39+
public override void WriteResponseContentHeaders(OutputFormatterContext context)
40+
{
41+
// Ignore the accept-charset field, as this will always write utf-8.
42+
var response = context.ActionContext.HttpContext.Response;
43+
response.ContentType = "text/plain;charset=utf-8";
44+
}
45+
46+
public override async Task WriteResponseBodyAsync(OutputFormatterContext context)
47+
{
48+
var response = context.ActionContext.HttpContext.Response;
49+
var valueAsString = context.Object as string;
50+
await response.WriteAsync(valueAsString);
51+
}
52+
}
53+
}

src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Compile Include="Extensions\ViewEngineDescriptorExtensions.cs" />
3434
<Compile Include="Formatters\DefaultOutputFormattersProvider.cs" />
3535
<Compile Include="Formatters\IOutputFormatter.cs" />
36+
<Compile Include="Formatters\TextPlainFormatter.cs" />
3637
<Compile Include="Formatters\StringWithQualityHeaderValueComparer.cs" />
3738
<Compile Include="IExpiringFileInfoCache.cs" />
3839
<Compile Include="Formatters\IOutputFormattersProvider.cs" />

src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void Setup(MvcOptions options)
3333
options.ModelBinders.Add(new ComplexModelDtoModelBinder());
3434

3535
// Set up default output formatters.
36+
options.OutputFormatters.Add(new TextPlainFormatter());
3637
options.OutputFormatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true));
3738

3839
// Set up ValueProviders

test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ObjectContentResultTests.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,29 +309,29 @@ public async Task ObjectResult_NoFormatterFound_Returns406()
309309
httpResponse.VerifySet(r => r.StatusCode = 406);
310310
}
311311

312-
// TODO: Disabling since this scenario is no longer dealt with in object result.
313-
// Re-enable once we do.
314-
//[Fact]
312+
[Fact]
315313
public async Task ObjectResult_Execute_CallsContentResult_SetsContent()
316314
{
317315
// Arrange
318-
var expectedContentType = "text/plain";
316+
var expectedContentType = "text/plain;charset=utf-8";
319317
var input = "testInput";
320318
var stream = new MemoryStream();
321319

322320
var httpResponse = new Mock<HttpResponse>();
323-
var tempContentType = string.Empty;
324321
httpResponse.SetupProperty<string>(o => o.ContentType);
325322
httpResponse.SetupGet(r => r.Body).Returns(stream);
326323

327-
var actionContext = CreateMockActionContext(httpResponse.Object);
324+
var actionContext = CreateMockActionContext(httpResponse.Object,
325+
requestAcceptHeader: null,
326+
requestContentType: null);
328327

329328
// Act
330329
var result = new ObjectResult(input);
331330
await result.ExecuteResultAsync(actionContext);
332331

333332
// Assert
334333
httpResponse.VerifySet(r => r.ContentType = expectedContentType);
334+
335335
// The following verifies the correct Content was written to Body
336336
Assert.Equal(input.Length, httpResponse.Object.Body.Length);
337337
}
@@ -473,7 +473,10 @@ public IReadOnlyList<IOutputFormatter> OutputFormatters
473473
get
474474
{
475475
return new List<IOutputFormatter>()
476-
{ new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: true) };
476+
{
477+
new TextPlainFormatter(),
478+
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), indent: true)
479+
};
477480
}
478481
}
479482
}

test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public void Setup_SetsUpOutputFormatters()
7272
setup.Setup(mvcOptions);
7373

7474
// Assert
75-
Assert.Equal(1, mvcOptions.OutputFormatters.Count);
76-
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[0].OutputFormatter);
75+
Assert.Equal(2, mvcOptions.OutputFormatters.Count);
76+
Assert.IsType<TextPlainFormatter>(mvcOptions.OutputFormatters[0].OutputFormatter);
77+
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[1].OutputFormatter);
7778
}
7879
}
7980
}

0 commit comments

Comments
 (0)