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
parent
79361b6fa2
commit
c3085c6c98
|
@ -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:
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue