7
7
using System . Text ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
- using Microsoft . AspNet . Http ;
11
10
using Microsoft . AspNet . Mvc . Core ;
12
11
using Microsoft . AspNet . Mvc . HeaderValueAbstractions ;
13
12
@@ -16,19 +15,12 @@ namespace Microsoft.AspNet.Mvc
16
15
/// <summary>
17
16
/// Writes an object to the output stream.
18
17
/// </summary>
19
- public abstract class OutputFormatter
18
+ public abstract class OutputFormatter : IOutputFormatter
20
19
{
21
- /// <summary>
22
- /// Gets the mutable collection of character encodings supported by
23
- /// this <see cref="OutputFormatter"/> instance. The encodings are
24
- /// used when writing the data.
25
- /// </summary>
20
+ /// <inheritdoc />
26
21
public List < Encoding > SupportedEncodings { get ; private set ; }
27
22
28
- /// <summary>
29
- /// Gets the mutable collection of <see cref="MediaTypeHeaderValue"/> elements supported by
30
- /// this <see cref="OutputFormatter"/> instance.
31
- /// </summary>
23
+ /// <inheritdoc />
32
24
public List < MediaTypeHeaderValue > SupportedMediaTypes { get ; private set ; }
33
25
34
26
/// <summary>
@@ -49,11 +41,12 @@ protected OutputFormatter()
49
41
/// <returns>The <see cref="Encoding"/> to use when reading the request or writing the response.</returns>
50
42
public virtual Encoding SelectCharacterEncoding ( OutputFormatterContext context )
51
43
{
52
- var encoding = MatchAcceptCharacterEncoding ( context . ActionContext . HttpContext . Request . AcceptCharset ) ;
44
+ var request = context . ActionContext . HttpContext . Request ;
45
+ var encoding = MatchAcceptCharacterEncoding ( request . AcceptCharset ) ;
53
46
if ( encoding == null )
54
47
{
55
48
// Match based on request acceptHeader.
56
- var requestContentType = MediaTypeHeaderValue . Parse ( context . ActionContext . HttpContext . Request . ContentType ) ;
49
+ var requestContentType = MediaTypeHeaderValue . Parse ( request . ContentType ) ;
57
50
if ( requestContentType != null && ! string . IsNullOrEmpty ( requestContentType . Charset ) )
58
51
{
59
52
var requestCharset = requestContentType . Charset ;
@@ -66,54 +59,8 @@ public virtual Encoding SelectCharacterEncoding(OutputFormatterContext context)
66
59
encoding = encoding ?? SupportedEncodings . FirstOrDefault ( ) ;
67
60
return encoding ;
68
61
}
69
-
70
- /// <summary>
71
- /// Sets the content-type headers with charset value to the HttpResponse.
72
- /// </summary>
73
- /// <param name="context">The formatter context associated with the call.</param>
74
- public virtual void SetResponseContentHeaders ( OutputFormatterContext context )
75
- {
76
- var selectedMediaType = context . SelectedContentType ;
77
-
78
- // If content type is not set then set it based on supported media types.
79
- selectedMediaType = selectedMediaType ?? SupportedMediaTypes . FirstOrDefault ( ) ;
80
- if ( selectedMediaType == null )
81
- {
82
- throw new InvalidOperationException ( Resources . FormatOutputFormatterNoMediaType ( GetType ( ) . FullName ) ) ;
83
- }
84
-
85
- if ( selectedMediaType != null && selectedMediaType . Charset == null )
86
- {
87
- // If content type charset parameter is not set then set it based on the supported encodings.
88
- var selectedEncoding = SelectCharacterEncoding ( context ) ;
89
- if ( selectedEncoding == null )
90
- {
91
- // No supported encoding was found so there is no way for us to start writing.
92
- throw new InvalidOperationException ( Resources . FormatOutputFormatterNoEncoding ( GetType ( ) . FullName ) ) ;
93
- }
94
-
95
- context . SelectedEncoding = selectedEncoding ;
96
- selectedMediaType . Charset = selectedEncoding . WebName ;
97
- }
98
-
99
- var response = context . ActionContext . HttpContext . Response ;
100
- response . ContentType = selectedMediaType . RawValue ;
101
- }
102
-
103
- /// <summary>
104
- /// Determines whether this <see cref="OutputFormatter"/> can serialize
105
- /// an object of the specified type.
106
- /// </summary>
107
- /// <param name="context">The formatter context associated with the call.</param>
108
- /// <param name="contentType">The desired contentType on the response.</param>
109
- /// <remarks>
110
- /// Subclasses can override this method to determine if the given content can be handled by this formatter.
111
- /// Subclasses should call the base implementation.
112
- /// </remarks>
113
- /// <returns>True if this <see cref="OutputFormatter"/> is able to serialize the object
114
- /// represent by <paramref name="context"/>'s ObjectResult and supports the passed in
115
- /// <paramref name="contentType"/>.
116
- /// False otherwise.</returns>
62
+
63
+ /// <inheritdoc />
117
64
public virtual bool CanWriteResult ( OutputFormatterContext context , MediaTypeHeaderValue contentType )
118
65
{
119
66
MediaTypeHeaderValue mediaType = null ;
@@ -140,12 +87,52 @@ public virtual bool CanWriteResult(OutputFormatterContext context, MediaTypeHead
140
87
return false ;
141
88
}
142
89
90
+ /// <inheritdoc />
91
+ public async Task WriteAsync ( OutputFormatterContext context , CancellationToken cancellationToken )
92
+ {
93
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
94
+ WriteResponseContentHeaders ( context ) ;
95
+ await WriteResponseBodyAsync ( context , cancellationToken ) ;
96
+ }
97
+
98
+ /// <summary>
99
+ /// Sets the content-type headers with charset value to the HttpResponse.
100
+ /// </summary>
101
+ /// <param name="context">The formatter context associated with the call.</param>
102
+ public virtual void WriteResponseContentHeaders ( OutputFormatterContext context )
103
+ {
104
+ var selectedMediaType = context . SelectedContentType ;
105
+
106
+ // If content type is not set then set it based on supported media types.
107
+ selectedMediaType = selectedMediaType ?? SupportedMediaTypes . FirstOrDefault ( ) ;
108
+ if ( selectedMediaType == null )
109
+ {
110
+ throw new InvalidOperationException ( Resources . FormatOutputFormatterNoMediaType ( GetType ( ) . FullName ) ) ;
111
+ }
112
+
113
+ var selectedEncoding = SelectCharacterEncoding ( context ) ;
114
+ if ( selectedEncoding == null )
115
+ {
116
+ // No supported encoding was found so there is no way for us to start writing.
117
+ throw new InvalidOperationException ( Resources . FormatOutputFormatterNoEncoding ( GetType ( ) . FullName ) ) ;
118
+ }
119
+
120
+ context . SelectedEncoding = selectedEncoding ;
121
+
122
+ // Override the content type value even if one already existed.
123
+ selectedMediaType . Charset = selectedEncoding . WebName ;
124
+ var response = context . ActionContext . HttpContext . Response ;
125
+ response . ContentType = selectedMediaType . RawValue ;
126
+ }
127
+
143
128
/// <summary>
144
- /// Writes given <paramref name="value"/> to the HttpResponse <paramref name=" response"/> body stream.
129
+ /// Writes the response body.
145
130
/// </summary>
131
+ /// <param name="context">The formatter context assoicated with the call.</param>
146
132
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
147
- /// <returns>A Task that serializes the value to the <paramref name="context"/>'s response message.</returns>
148
- public abstract Task WriteAsync ( OutputFormatterContext context , CancellationToken cancellationToken ) ;
133
+ /// <returns></returns>
134
+ public abstract Task WriteResponseBodyAsync ( OutputFormatterContext context ,
135
+ CancellationToken cancellationToken ) ;
149
136
150
137
private Encoding MatchAcceptCharacterEncoding ( string acceptCharsetHeader )
151
138
{
0 commit comments