Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aspnetcore/release-notes/aspnetcore-11.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ This section describes new features for Minimal APIs.

This section describes new features for OpenAPI.

[!INCLUDE[](~/release-notes/aspnetcore-11/includes/openapi-binary-file-response.md)]
Comment thread
wadepickett marked this conversation as resolved.

## Authentication and authorization

This section describes new features for authentication and authorization.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
### Describe binary file responses

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`.

#### [Minimal APIs](#tab/minimal-apis)

Use the `Produces<T>` extension method with `T` of `FileContentResult` to specify the response type and content type:
<!-- UPDATE 11.0 - API cross-link needs to be entered in the line above for the new API
such as: <xref:Microsoft.AspNetCore.NEW_API_TO_BE_ENTERED_>
-->
Comment thread
wadepickett marked this conversation as resolved.

```csharp
app.MapPost("/filecontentresult", () =>
{
var content = "This endpoint returns a FileContentResult!"u8.ToArray();
return TypedResults.File(content);
})
.Produces<FileContentResult>(contentType: MediaTypeNames.Application.Octet);
```

#### [Controllers](#tab/controllers)

Use the `ProducesResponseType<T>` attribute with `T` of `FileContentResult` to specify the response type and content type:

```csharp
[HttpPost("filecontentresult")]
[ProducesResponseType<FileContentResult>(StatusCodes.Status200OK, MediaTypeNames.Application.Octet)]
public IActionResult PostFileContentResult()
{
var content = "This endpoint returns a FileContentResult!"u8.ToArray();
return new FileContentResult(content, MediaTypeNames.Application.Octet);
}
```

---

The generated OpenAPI document describes the endpoint response as:

```yaml
responses:
'200':
description: OK
content:
application/octet-stream:
schema:
$ref: '#/components/schemas/FileContentResult'
```

The `FileContentResult` is defined in `components/schemas` as:

```yaml
components:
schemas:
FileContentResult:
type: string
format: binary
```