Skip to content

Commit 1a3bab3

Browse files
[11.0 P1] What's New: OpenAPI: Binary File Response (#36747)
* [11.0 P1] What's New: OpenAPI: Binary File Response * Update aspnetcore/release-notes/aspnetcore-11/includes/openapi-binary-file-response.md Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com> * Update aspnetcore/release-notes/aspnetcore-11/includes/openapi-binary-file-response.md Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com> --------- Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com>
1 parent 0c5ad46 commit 1a3bab3

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

aspnetcore/release-notes/aspnetcore-11.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ This section describes new features for Minimal APIs.
3737

3838
This section describes new features for OpenAPI.
3939

40+
[!INCLUDE[](~/release-notes/aspnetcore-11/includes/openapi-binary-file-response.md)]
41+
4042
## Authentication and authorization
4143

4244
This section describes new features for authentication and authorization.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### Describe binary file responses
2+
3+
ASP.NET Core 11 introduces support for generating OpenAPI descriptions for operations that return binary file responses. This support maps the `FileContentResult` result type to an OpenAPI schema with `type: string` and `format: binary`.
4+
5+
#### [Minimal APIs](#tab/minimal-apis)
6+
7+
Use the `Produces<T>` extension method with `T` of `FileContentResult` to specify the response type and content type:
8+
<!-- UPDATE 11.0 - API cross-link needs to be entered in the line above for the new API
9+
such as: <xref:Microsoft.AspNetCore.NEW_API_TO_BE_ENTERED_>
10+
-->
11+
12+
```csharp
13+
app.MapPost("/filecontentresult", () =>
14+
{
15+
var content = "This endpoint returns a FileContentResult!"u8.ToArray();
16+
return TypedResults.File(content);
17+
})
18+
.Produces<FileContentResult>(contentType: MediaTypeNames.Application.Octet);
19+
```
20+
21+
#### [Controllers](#tab/controllers)
22+
23+
Use the `ProducesResponseType<T>` attribute with `T` of `FileContentResult` to specify the response type and content type:
24+
25+
```csharp
26+
[HttpPost("filecontentresult")]
27+
[ProducesResponseType<FileContentResult>(StatusCodes.Status200OK, MediaTypeNames.Application.Octet)]
28+
public IActionResult PostFileContentResult()
29+
{
30+
var content = "This endpoint returns a FileContentResult!"u8.ToArray();
31+
return new FileContentResult(content, MediaTypeNames.Application.Octet);
32+
}
33+
```
34+
35+
---
36+
37+
The generated OpenAPI document describes the endpoint response as:
38+
39+
```yaml
40+
responses:
41+
'200':
42+
description: OK
43+
content:
44+
application/octet-stream:
45+
schema:
46+
$ref: '#/components/schemas/FileContentResult'
47+
```
48+
49+
The `FileContentResult` is defined in `components/schemas` as:
50+
51+
```yaml
52+
components:
53+
schemas:
54+
FileContentResult:
55+
type: string
56+
format: binary
57+
```

0 commit comments

Comments
 (0)