Skip to content

Commit f568987

Browse files
Update request-response.md (#35289)
* Update request-response.md * Apply suggestions from code review Co-authored-by: Günther Foidl <[email protected]> * response body * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md Co-authored-by: Günther Foidl <[email protected]> * Update aspnetcore/fundamentals/middleware/request-response.md * Update aspnetcore/fundamentals/middleware/request-response.md --------- Co-authored-by: Günther Foidl <[email protected]>
1 parent a880b5c commit f568987

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

aspnetcore/fundamentals/middleware/request-response.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to read the request body and write the response body in A
55
monikerRange: '>= aspnetcore-3.0'
66
ms.author: tdykstra
77
ms.custom: mvc
8-
ms.date: 5/29/2019
8+
ms.date: 4/22/2025
99
uid: fundamentals/middleware/request-response
1010
---
1111
# Request and response operations in ASP.NET Core
@@ -25,7 +25,10 @@ There are two abstractions for the request and response bodies: <xref:System.IO.
2525
* `TextWriter`
2626
* `HttpResponse.WriteAsync`
2727

28-
Streams aren't being removed from the framework. Streams continue to be used throughout .NET, and many stream types don't have pipe equivalents, such as `FileStreams` and `ResponseCompression`.
28+
Streams aren't being removed from the framework. Streams continue to be used throughout .NET:
29+
30+
* Many stream types don't have pipe equivalents, such as `FileStreams` and `ResponseCompression`.
31+
* It's straightforward adding compression to a stream.
2932

3033
## Stream examples
3134

@@ -70,7 +73,7 @@ These issues are fixable, but the code is becoming progressively more complicate
7073

7174
## Pipelines
7275

73-
The following example shows how the same scenario can be handled using a [PipeReader](/dotnet/standard/io/pipelines#pipe):
76+
The following example shows how the preceding stream scenario can be handled using a [PipeReader](/dotnet/standard/io/pipelines#pipe):
7477

7578
[!code-csharp[](request-response/samples/3.x/RequestResponseSample/Startup.cs?name=GetListOfStringFromPipe)]
7679

@@ -80,6 +83,13 @@ This example fixes many issues that the streams implementations had:
8083
* Encoded strings are directly added to the list of returned strings.
8184
* Other than the `ToArray` call, and the memory used by the string, string creation is allocation free.
8285

86+
When writing directly to `HttpResponse.BodyWriter`, call `PipeWriter.FlushAsync` manually to ensure the data is flushed to the underlying response response body. Here's why:
87+
88+
* `HttpResponse.BodyWriter` is a `PipeWriter` that buffers data until a flush operation is triggered.
89+
* Calling `FlushAsync` writes the buffered data to the underlying response body.
90+
91+
It's up to the developer to decide when to call `FlushAsync`, balancing factors such as buffer size, network write overhead, and whether the data should be sent in discrete chunks. For more information, see [System.IO.Pipelines in .NET](/dotnet/standard/io/pipelines).
92+
8393
## Adapters
8494

8595
The `Body`, `BodyReader`, and `BodyWriter` properties are available for `HttpRequest` and `HttpResponse`. When you set `Body` to a different stream, a new set of adapters automatically adapt each type to the other. If you set `HttpRequest.Body` to a new stream, `HttpRequest.BodyReader` is automatically set to a new `PipeReader` that wraps `HttpRequest.Body`.

0 commit comments

Comments
 (0)