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
18 changes: 17 additions & 1 deletion aspnetcore/fundamentals/minimal-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Provides an overview of minimal APIs in ASP.NET Core
ms.author: wpickett
content_well_notification: AI-contribution
monikerRange: '>= aspnetcore-6.0'
ms.date: 10/23/2023
ms.date: 02/07/2024
uid: fundamentals/minimal-apis
ai-usage: ai-assisted
---
Expand Down Expand Up @@ -171,6 +171,22 @@ app.MapGet("/download", () => Results.File("myfile.text"));

[!INCLUDE [results-helpers](~/fundamentals/minimal-apis/includes/results-helpers.md)]

### Modifying Headers

Use the `HttpResponse` object to modify response headers:

```csharp
app.MapGet("/", (HttpContext context) => {
// Set a custom header
context.Response.Headers["X-Custom-Header"] = "CustomValue";

// Set a known header
context.Response.Headers.CacheControl = $"public,max-age=3600";

return "Hello World";
});
```

### Customizing results

Applications can control responses by implementing a custom <xref:Microsoft.AspNetCore.Http.IResult> type. The following code is an example of an HTML result type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public static void Main(string[] args)
// <snippet_10>
app.MapGet("/download", () => Results.File("myfile.text"));
// </snippet_10>

// <snippet_12>
app.MapGet("/problem", () =>
{
var extensions = new List<KeyValuePair<string, object?>> { new("test", "value") };
return TypedResults.Problem("This is an error with extensions",
extensions: extensions);
});
// </snippet_12>
app.Run();
}
// <snippet_11>
Expand Down
22 changes: 21 additions & 1 deletion aspnetcore/fundamentals/minimal-apis/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author: brunolins16
description: Learn how to create responses for minimal APIs in ASP.NET Core.
ms.author: brolivei
monikerRange: '>= aspnetcore-7.0'
ms.date: 06/04/2024
ms.date: 02/07/2025
uid: fundamentals/minimal-apis/responses
---

Expand Down Expand Up @@ -146,6 +146,10 @@ The following sections demonstrate the usage of the common result helpers.

The preceding example returns a 500 status code.

#### Problem and ValidationProblem

:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_12":::

#### Text

:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_08":::
Expand Down Expand Up @@ -195,6 +199,22 @@ Here's an example of a filter that uses one of these interfaces:

For more information, see [Filters in Minimal API apps](xref:fundamentals/minimal-apis/min-api-filters) and [IResult implementation types](xref:fundamentals/minimal-apis/test-min-api#iresult-implementation-types).

## Modifying Headers

Use the `HttpResponse` object to modify response headers:

```csharp
app.MapGet("/", (HttpContext context) => {
// Set a custom header
context.Response.Headers["X-Custom-Header"] = "CustomValue";

// Set a known header
context.Response.Headers.CacheControl = $"public,max-age=3600";

return "Hello World";
});
```

## Customizing responses

Applications can control responses by implementing a custom <xref:Microsoft.AspNetCore.Http.IResult> type. The following code is an example of an HTML result type:
Expand Down