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

Commit d3055f7

Browse files
committed
updated
1 parent fbb152a commit d3055f7

File tree

3 files changed

+24
-62
lines changed

3 files changed

+24
-62
lines changed

src/Microsoft.AspNet.Mvc.Core/ActionResults/ContentResult.cs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ContentResult : ActionResult
1515
{
1616
private readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/plain")
1717
{
18-
Charset = Encodings.UTF8EncodingWithoutBOM.WebName
18+
Encoding = Encoding.UTF8
1919
};
2020

2121
public string Content { get; set; }
@@ -31,45 +31,24 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
3131
{
3232
var response = context.HttpContext.Response;
3333

34-
MediaTypeHeaderValue contentTypeHeader = ContentType;
34+
var contentTypeHeader = ContentType;
3535
Encoding encoding;
36-
if(contentTypeHeader == null)
36+
if (contentTypeHeader == null)
3737
{
3838
contentTypeHeader = DefaultContentType;
39-
encoding = Encodings.UTF8EncodingWithoutBOM;
39+
encoding = DefaultContentType.Encoding;
4040
}
4141
else
4242
{
43-
if(string.IsNullOrEmpty(contentTypeHeader.Charset))
43+
if (contentTypeHeader.Encoding == null)
4444
{
45-
encoding = Encodings.UTF8EncodingWithoutBOM;
46-
4745
// 1. Do not modify the user supplied content type
4846
// 2. Parse here to handle parameters apart from charset
4947
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
50-
contentTypeHeader.Charset = encoding.WebName;
51-
}
52-
else
53-
{
54-
if (string.Equals(
55-
contentTypeHeader.Charset,
56-
Encodings.UTF8EncodingWithoutBOM.WebName,
57-
StringComparison.OrdinalIgnoreCase))
58-
{
59-
encoding = Encodings.UTF8EncodingWithoutBOM;
60-
}
61-
else if (string.Equals(
62-
contentTypeHeader.Charset,
63-
Encodings.UTF16EncodingLittleEndian.WebName,
64-
StringComparison.OrdinalIgnoreCase))
65-
{
66-
encoding = Encodings.UTF16EncodingLittleEndian;
67-
}
68-
else
69-
{
70-
encoding = new ResponseEncodingWrapper(Encoding.GetEncoding(contentTypeHeader.Charset));
71-
}
48+
contentTypeHeader.Encoding = Encoding.UTF8;
7249
}
50+
51+
encoding = contentTypeHeader.Encoding;
7352
}
7453

7554
response.ContentType = contentTypeHeader.ToString();
@@ -81,7 +60,7 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
8160

8261
if (Content != null)
8362
{
84-
await response.WriteAsync(Content, encoding);
63+
await response.WriteAsync(Content, new ResponseEncodingWrapper(encoding));
8564
}
8665
}
8766
}

src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewExecutor.cs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class ViewExecutor
1919
private const int BufferSize = 1024;
2020
private static readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/html")
2121
{
22-
Charset = Encodings.UTF8EncodingWithoutBOM.WebName
22+
Encoding = Encoding.UTF8
2323
};
2424

2525
/// <summary>
@@ -38,52 +38,35 @@ public static async Task ExecuteAsync([NotNull] IView view,
3838
{
3939
var response = actionContext.HttpContext.Response;
4040

41-
MediaTypeHeaderValue contentTypeHeader = contentType;
41+
var contentTypeHeader = contentType;
4242
Encoding encoding;
4343
if (contentTypeHeader == null)
4444
{
4545
contentTypeHeader = DefaultContentType;
46-
encoding = Encodings.UTF8EncodingWithoutBOM;
46+
encoding = DefaultContentType.Encoding;
4747
}
4848
else
4949
{
50-
if (string.IsNullOrEmpty(contentTypeHeader.Charset))
50+
if (contentTypeHeader.Encoding == null)
5151
{
52-
encoding = Encodings.UTF8EncodingWithoutBOM;
53-
5452
// 1. Do not modify the user supplied content type
5553
// 2. Parse here to handle parameters apart from charset
5654
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
57-
contentTypeHeader.Charset = encoding.WebName;
58-
}
59-
else
60-
{
61-
if(string.Equals(
62-
contentTypeHeader.Charset,
63-
Encodings.UTF8EncodingWithoutBOM.WebName,
64-
StringComparison.OrdinalIgnoreCase))
65-
{
66-
encoding = Encodings.UTF8EncodingWithoutBOM;
67-
}
68-
else if(string.Equals(
69-
contentTypeHeader.Charset,
70-
Encodings.UTF16EncodingLittleEndian.WebName,
71-
StringComparison.OrdinalIgnoreCase))
72-
{
73-
encoding = Encodings.UTF16EncodingLittleEndian;
74-
}
75-
else
76-
{
77-
encoding = new ResponseEncodingWrapper(Encoding.GetEncoding(contentTypeHeader.Charset));
78-
}
55+
contentTypeHeader.Encoding = Encoding.UTF8;
7956
}
57+
58+
encoding = contentTypeHeader.Encoding;
8059
}
8160

8261
response.ContentType = contentTypeHeader.ToString();
8362

8463
var wrappedStream = new StreamWrapper(response.Body);
8564

86-
using (var writer = new StreamWriter(wrappedStream, encoding, BufferSize, leaveOpen: true))
65+
using (var writer = new StreamWriter(
66+
wrappedStream,
67+
new ResponseEncodingWrapper(encoding),
68+
BufferSize,
69+
leaveOpen: true))
8770
{
8871
try
8972
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding(
6464
var contentResult = new ContentResult
6565
{
6666
Content = null,
67-
ContentType = new MediaTypeHeaderValue("application/json")
67+
ContentType = new MediaTypeHeaderValue("text/plain")
6868
{
69-
Encoding = Encoding.UTF8
69+
Encoding = Encoding.UTF7
7070
}
7171
};
7272
var httpContext = GetHttpContext();
@@ -76,7 +76,7 @@ public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding(
7676
await contentResult.ExecuteResultAsync(actionContext);
7777

7878
// Assert
79-
Assert.Equal("application/json; charset=utf-8", httpContext.Response.ContentType);
79+
Assert.Equal("text/plain; charset=utf-7", httpContext.Response.ContentType);
8080
}
8181

8282
public static TheoryData<MediaTypeHeaderValue, string, byte[]> ContentResultContentTypeData

0 commit comments

Comments
 (0)