Add minimal API sample of reading the request stream (#24327)

Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com>
Co-authored-by: Stephen Halter <halter73@gmail.com>
pull/24321/head
James Newton-King 2021-12-16 11:25:08 +13:00 committed by GitHub
parent 79361b6fa2
commit c3085c6c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -597,6 +597,17 @@ The preceding code:
}
```
### Read the request body
Read the request body directly using a <xref:Microsoft.AspNetCore.Http.HttpContext> or <xref:Microsoft.AspNetCore.Http.HttpRequest> parameter:
[!code-csharp[](minimal-apis/samples/WebMinAPIs/Program.cs?name=snippet_fileupload)]
The preceding code:
* Accesses the request body using <xref:Microsoft.AspNetCore.Http.HttpRequest.BodyReader?displayProperty=nameWithType>.
* Copies the request body to a local file.
## Responses
Route handlers support the following types of return values:

View File

@ -813,4 +813,20 @@ app.MapGet("/skipme", () => "Skipping Swagger.")
app.Run();
#endregion
#elif FILEUPLOAD
#region snippet_fileupload
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/uploadstream", async (IConfiguration config, HttpRequest request) =>
{
var filePath = Path.Combine(config["StoredFilesPath"], Path.GetRandomFileName());
await using var writeStream = File.Create(filePath);
await request.BodyReader.CopyToAsync(writeStream);
});
app.Run();
#endregion
#endif