Skip to content
57 changes: 49 additions & 8 deletions aspnetcore/fundamentals/minimal-apis/responses.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
title: Create responses in Minimal API applications
ai-usage: ai-assisted
description: Learn how to create responses for Minimal APIs in ASP.NET Core.
ms.reviewer: brolivei
monikerRange: '>= aspnetcore-7.0'
ms.date: 06/16/2026
ms.date: 10/21/2026
uid: fundamentals/minimal-apis/responses
ai-usage: ai-assisted
---

# How to create responses in Minimal API apps
Expand Down Expand Up @@ -280,23 +280,64 @@ the CLR type of the content in the file result in order to get the desired OpenA

In particular, there are three common schemas for file results:

- **binary content**, such as PDFs, images, or videos, where the schema should be `type: string, format: binary`.
The recommended `TResponse` for this case is `Stream`. The framework has special logic to map
this type to the `binary` format in the OpenAPI schema.
<!-- Hope we can change this to IBinaryContent if #67145 is approved and implemented -->
- **text content**, such as CSV or plain text. Here the schema should be simply `type: string` with no `format`. Use `string` as the `TResponse` for this case.
- **base64-encoded content**, where the schema should be `type: string, format: byte`. It's uncommon to base64-encode file content in an API response, but for legacy reasons this is the schema produced when the `TResponse` is `byte[]`.
:::moniker-end

:::moniker range=">= aspnetcore-10.0 < aspnetcore-11.0"

* **binary content**, such as PDFs, images, or videos, where the schema should be `type: string, format: binary`. The recommended `TResponse` for this case is `Stream`. The framework has special logic to map this type to the `binary` format in the OpenAPI schema.
* **text content**, such as CSV or plain text. Here the schema should be simply `type: string` with no `format`. Use `string` as the `TResponse` for this case.
* **base64-encoded content**, where the schema should be `type: string, format: byte`. It's uncommon to base64-encode file content in an API response, but for legacy reasons this is the schema produced when the `TResponse` is `byte[]`.

:::moniker-end

:::moniker range=">= aspnetcore-11.0"

<!-- UPDATE 12.0 - For the first bullet of the 11.0 or greater list, keep an eye on https://github.com/dotnet/aspnetcore/issues/67145 (IBinaryContent). -->

* **binary content**, such as PDFs, images, or videos, where the schema should be `type: string, format: binary`. The recommended `TResponse` for this case is `Stream`. The framework has special logic to map this type to the `binary` format in the OpenAPI schema. For a working example that shows the generated OpenAPI document for a binary response, see [Describe binary file responses](xref:fundamentals/openapi/include-metadata#describe-binary-file-responses).
* **text content**, such as CSV or plain text. Here the schema should be simply `type: string` with no `format`. Use `string` as the `TResponse` for this case.
* **base64-encoded content**, where the schema should be `type: string, format: byte`. It's uncommon to base64-encode file content in an API response, but for legacy reasons this is the schema produced when the `TResponse` is `byte[]`.

The file result types themselves also map to the `type: string, format: binary` schema, so `TResponse` can match what the endpoint actually returns:

```csharp
app.MapGet("/report", () => TypedResults.File(File.OpenRead("report.pdf"), MediaTypeNames.Application.Pdf))
.Produces<FileStreamHttpResult>(contentType: MediaTypeNames.Application.Pdf);
```

<xref:Microsoft.AspNetCore.Http.HttpResults.FileContentHttpResult>, <xref:Microsoft.AspNetCore.Http.HttpResults.FileStreamHttpResult>, <xref:Microsoft.AspNetCore.Mvc.FileContentResult>, and <xref:Microsoft.AspNetCore.Mvc.FileStreamResult> all produce the binary schema. `Stream` continues to work and remains a good choice when the result type isn't known at the point where the metadata is declared.

:::moniker-end

:::moniker range=">= aspnetcore-10.0"

In a controller-based app, use the [`[Produces]`](xref:Microsoft.AspNetCore.Mvc.ProducesAttribute) or the [`[ProducesResponseType]`](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute) attribute:

:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/minimal-apis/FileResults/10.x/Controllers/FilesController.cs" id="snippet_openapi":::

As with Minimal APIs, you should choose the `Type` parameter of the `[Produces]` and `[ProducesResponseType]` attributes
that corresponds to the desired OpenAPI schema for your file result responses:

:::moniker-end

:::moniker range=">= aspnetcore-11.0"

* **binary content**: Use `FileContentResult` or `FileStreamResult` to get the `binary` format in the OpenAPI schema.
* **text content**: Use `string` to get a simple `type: string` schema.
* **base64-encoded content**: Use `byte[]` to get the `byte` format in the OpenAPI schema, though this is uncommon for file results.

:::moniker-end

:::moniker range=">= aspnetcore-10.0 < aspnetcore-11.0"

* **binary content**: Use `Stream` to get the `binary` format in the OpenAPI schema.
* **text content**: Use `string` to get a simple `type: string` schema.
* **base64-encoded content**: Use `byte[]` to get the `byte` format in the OpenAPI schema, though this is uncommon for file results.

:::moniker-end

:::moniker range=">= aspnetcore-10.0"

### File result support for conditional requests

File results support [conditional requests](https://www.rfc-editor.org/rfc/rfc9110#section-13) for cache validation. Set the `lastModified` and/or `entityTag` parameters when creating the result object, and the framework automatically inspects incoming `If-None-Match` and `If-Modified-Since` headers. If the resource hasn't changed, the framework returns `304 Not Modified` with no body — no additional code is needed.
Expand Down
11 changes: 7 additions & 4 deletions aspnetcore/fundamentals/openapi/include-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ title: Include OpenAPI metadata in an ASP.NET Core app
ai-usage: ai-assisted
description: Learn how to add OpenAPI metadata in an ASP.NET Core app.
monikerRange: '>= aspnetcore-9.0'
ms.date: 10/20/2026
ms.date: 10/21/2026

uid: fundamentals/openapi/include-metadata
---
# Include OpenAPI metadata in an ASP.NET Core app
Expand Down Expand Up @@ -366,7 +367,7 @@ A class can be implemented to set the endpoint metadata and return it from the r

##### Describe binary file responses

To describe endpoints that return binary file responses in the OpenAPI document, use the <xref:Microsoft.AspNetCore.Http.OpenApiRouteHandlerBuilderExtensions.Produces%2A> extension method with `FileContentResult` as the type parameter to specify the response type and content type:
To describe endpoints that return binary file responses in the OpenAPI document, use the <xref:Microsoft.AspNetCore.Http.OpenApiRouteHandlerBuilderExtensions.Produces%2A> extension method with a file result type as the type parameter to specify the response type and content type. <xref:Microsoft.AspNetCore.Mvc.FileContentResult>, <xref:Microsoft.AspNetCore.Mvc.FileStreamResult>, <xref:Microsoft.AspNetCore.Http.HttpResults.FileContentHttpResult>, and <xref:Microsoft.AspNetCore.Http.HttpResults.FileStreamHttpResult> all produce a binary schema. The following example uses `FileContentResult`:

```csharp
app.MapPost("/filecontentresult", () =>
Expand All @@ -377,7 +378,7 @@ app.MapPost("/filecontentresult", () =>
.Produces<FileContentResult>(contentType: MediaTypeNames.Application.Octet);
```

This generates an OpenAPI schema with `type: string` and `format: binary` for the `FileContentResult` type.
This generates an OpenAPI schema with `type: string` and `format: binary` for the `FileContentResult` type. The other file result types behave identically, each with a component schema named after the type—for example, `FileStreamHttpResult`.

The generated OpenAPI document describes the endpoint response as:

Expand All @@ -401,6 +402,8 @@ components:
format: binary
```

File results also support conditional requests and range requests, and `Stream` is an equally valid `TResponse` for a binary schema. For the full set of file result types and their behavior, see [File result return values](xref:fundamentals/minimal-apis/responses#file-result-return-values).

##### Set responses for `ProblemDetails`

When setting the response type for endpoints that may return a ProblemDetails response, the following can be used to add the appropriate response metadata for the endpoint:
Expand Down Expand Up @@ -494,7 +497,7 @@ public IActionResult PostFileContentResult()
}
```

This operation generates the same OpenAPI description as the [Minimal API binary file response example](xref:fundamentals/openapi/include-metadata#describe-binary-file-responses), with `FileContentResult` defined as `type: string` and `format: binary`.
This operation generates the same OpenAPI description as the [Minimal API binary file response example](xref:fundamentals/openapi/include-metadata#describe-binary-file-responses) with `FileContentResult` defined as `type: string` and `format: binary`. <xref:Microsoft.AspNetCore.Mvc.FileStreamResult> behaves identically and produces a `FileStreamResult` component schema.

---

Expand Down
Loading