Add a variant of FileResult / Results.Stream that gives you access to the underlying response body
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
## Background and Motivation
## Proposed API
```diff
namespace Microsoft.AspNetCore.Http
{
public static class Results
{
+ public IResult Stream(this HttpResponse response, Func streamAction, string? contentType = null);
}
namespace Microsoft.AspNetCore.Mvc
{
public class ControllerBase
{
+ public StreamActionResult Stream(Func streamAction);
}
+ public class StreamActionResult : ActionResult
+ {
+ public StreamActionResult(Func streamAction, string? contentType);
+ public Func StreamAction { get; set; }
+ public Task ExecuteAsync(ActionResult actionResult);
+ }
```
## Usage Examples
```C#
app.MapGet("/blob", (BlobServiceClient client) =>
{
BlobClient blob = client.GetBlobContainerClient("container").GetBlobClient("blob");
return Results.Stream((stream, cancellationToken) => blob.DownloadToAsync(stream, cancellationToken), fileDownloadName: fileName));
});
```
```C#
public class TestController : ControllerBase
{
public IActionResult GetBlob()
{
BlobClient blob = client.GetBlobContainerClient("container").GetBlobClient("blob");
return Stream((stream, cancellationToken) => blob.DownloadToAsync(stream, cancellationToken), fileDownloadName: fileName));
}
}
```
Note that this does not expose conditional requests / range requests as supported by other File* / Stream based results.
## Risks
N/A
Contributor guide
Assessment
This issue has not been assessed yet.