@@ -47,8 +47,8 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
4747 {
4848 ArgumentGuard . NotNull ( context , nameof ( context ) ) ;
4949
50+ HttpRequest request = context . HttpContext . Request ;
5051 HttpResponse response = context . HttpContext . Response ;
51- response . ContentType = _serializer . ContentType ;
5252
5353 await using TextWriter writer = context . WriterFactory ( response . Body , Encoding . UTF8 ) ;
5454 string responseContent ;
@@ -67,23 +67,27 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
6767 response . StatusCode = ( int ) errorDocument . GetErrorStatusCode ( ) ;
6868 }
6969
70- bool hasMatchingETag = SetETagResponseHeader ( context . HttpContext . Request , response , responseContent ) ;
70+ bool hasMatchingETag = SetETagResponseHeader ( request , response , responseContent ) ;
7171
7272 if ( hasMatchingETag )
7373 {
7474 response . StatusCode = ( int ) HttpStatusCode . NotModified ;
7575 responseContent = string . Empty ;
7676 }
7777
78- if ( context . HttpContext . Request . Method == HttpMethod . Head . Method )
78+ if ( request . Method == HttpMethod . Head . Method )
7979 {
8080 responseContent = string . Empty ;
8181 }
8282
83- string url = context . HttpContext . Request . GetEncodedUrl ( ) ;
83+ string url = request . GetEncodedUrl ( ) ;
8484
85- _traceWriter . LogMessage ( ( ) =>
86- $ "Sending { response . StatusCode } response for { context . HttpContext . Request . Method } request at '{ url } ' with body: <<{ responseContent } >>") ;
85+ if ( ! string . IsNullOrEmpty ( responseContent ) )
86+ {
87+ response . ContentType = _serializer . ContentType ;
88+ }
89+
90+ _traceWriter . LogMessage ( ( ) => $ "Sending { response . StatusCode } response for { request . Method } request at '{ url } ' with body: <<{ responseContent } >>") ;
8791
8892 await writer . WriteAsync ( responseContent ) ;
8993 await writer . FlushAsync ( ) ;
0 commit comments