@@ -76,10 +76,6 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
7676 {
7777 return ;
7878 }
79- if ( ! endpoint . Response . HasNoResponse && endpoint . Response is { ContentType : { } contentType } )
80- {
81- codeWriter . WriteLine ( $@ "httpContext.Response.ContentType ??= ""{ contentType } "";") ;
82- }
8379 if ( ! endpoint . Response . HasNoResponse )
8480 {
8581 codeWriter . Write ( "var result = " ) ;
@@ -89,6 +85,9 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
8985 codeWriter . Write ( "await " ) ;
9086 }
9187 codeWriter . WriteLine ( $ "handler({ endpoint . EmitArgumentList ( ) } );") ;
88+
89+ endpoint . Response . EmitHttpResponseContentType ( codeWriter ) ;
90+
9291 if ( ! endpoint . Response . HasNoResponse )
9392 {
9493 codeWriter . WriteLine ( endpoint . Response . EmitResponseWritingCall ( endpoint . IsAwaitable ) ) ;
@@ -97,9 +96,27 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri
9796 {
9897 codeWriter . WriteLine ( "return Task.CompletedTask;" ) ;
9998 }
99+
100100 codeWriter . EndBlock ( ) ; // End handler method block
101101 }
102102
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+
103120 private static string EmitResponseWritingCall ( this EndpointResponse endpointResponse , bool isAwaitable )
104121 {
105122 var returnOrAwait = isAwaitable ? "await" : "return" ;
@@ -183,7 +200,7 @@ private static void EmitBuiltinResponseTypeMetadata(this Endpoint endpoint, Code
183200 }
184201 else
185202 {
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));") ;
187204 }
188205 }
189206
0 commit comments