@@ -76,10 +76,6 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
76
76
{
77
77
return ;
78
78
}
79
- if ( ! endpoint . Response . HasNoResponse && endpoint . Response is { ContentType : { } contentType } )
80
- {
81
- codeWriter . WriteLine ( $@ "httpContext.Response.ContentType ??= ""{ contentType } "";") ;
82
- }
83
79
if ( ! endpoint . Response . HasNoResponse )
84
80
{
85
81
codeWriter . Write ( "var result = " ) ;
@@ -89,6 +85,9 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
89
85
codeWriter . Write ( "await " ) ;
90
86
}
91
87
codeWriter . WriteLine ( $ "handler({ endpoint . EmitArgumentList ( ) } );") ;
88
+
89
+ endpoint . Response . EmitHttpResponseContentType ( codeWriter ) ;
90
+
92
91
if ( ! endpoint . Response . HasNoResponse )
93
92
{
94
93
codeWriter . WriteLine ( endpoint . Response . EmitResponseWritingCall ( endpoint . IsAwaitable ) ) ;
@@ -97,9 +96,27 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
97
96
{
98
97
codeWriter . WriteLine ( "return Task.CompletedTask;" ) ;
99
98
}
99
+
100
100
codeWriter . EndBlock ( ) ; // End handler method block
101
101
}
102
102
103
+ private static void EmitHttpResponseContentType ( this EndpointResponse endpointResponse , CodeWriter codeWriter )
104
+ {
105
+ if ( ! endpointResponse . HasNoResponse
106
+ && endpointResponse . ResponseType is { } responseType
107
+ && ( responseType . SpecialType == SpecialType . System_Object || responseType . SpecialType == SpecialType . System_String ) )
108
+ {
109
+ codeWriter . WriteLine ( "if (result is string)" ) ;
110
+ codeWriter . StartBlock ( ) ;
111
+ codeWriter . WriteLine ( $@ "httpContext.Response.ContentType ??= ""text/plain; charset=utf-8"";") ;
112
+ codeWriter . EndBlock ( ) ;
113
+ codeWriter . WriteLine ( "else" ) ;
114
+ codeWriter . StartBlock ( ) ;
115
+ codeWriter . WriteLine ( $@ "httpContext.Response.ContentType ??= ""application/json; charset=utf-8"";") ;
116
+ codeWriter . EndBlock ( ) ;
117
+ }
118
+ }
119
+
103
120
private static string EmitResponseWritingCall ( this EndpointResponse endpointResponse , bool isAwaitable )
104
121
{
105
122
var returnOrAwait = isAwaitable ? "await" : "return" ;
@@ -183,7 +200,7 @@ private static void EmitBuiltinResponseTypeMetadata(this Endpoint endpoint, Code
183
200
}
184
201
else
185
202
{
186
- codeWriter . WriteLine ( $ "options.EndpointBuilder.Metadata.Add(new GeneratedProducesResponseTypeMetadata(type: typeof({ responseType . ToDisplayString ( EmitterConstants . DisplayFormat ) } ), statusCode: StatusCodes.Status200OK, contentTypes: GeneratedMetadataConstants.JsonContentType));") ;
203
+ codeWriter . WriteLine ( $ "options.EndpointBuilder.Metadata.Add(new GeneratedProducesResponseTypeMetadata(type: typeof({ responseType . ToDisplayString ( EmitterConstants . DisplayFormatWithoutNullability ) } ), statusCode: StatusCodes.Status200OK, contentTypes: GeneratedMetadataConstants.JsonContentType));") ;
187
204
}
188
205
}
189
206
0 commit comments