Skip to content

Commit 683b981

Browse files
add example to modify headers in Minimal APIs (#34666)
* Add documentation for modifying response headers in Minimal APIs * Add example for Problem result in Minimal APIs * Update ms.date in minimal-apis.md * Update ms.date in responses.md --------- Co-authored-by: Wade Pickett <[email protected]>
1 parent 62f6fb0 commit 683b981

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

aspnetcore/fundamentals/minimal-apis.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Provides an overview of minimal APIs in ASP.NET Core
55
ms.author: wpickett
66
content_well_notification: AI-contribution
77
monikerRange: '>= aspnetcore-6.0'
8-
ms.date: 10/23/2023
8+
ms.date: 02/07/2024
99
uid: fundamentals/minimal-apis
1010
ai-usage: ai-assisted
1111
---
@@ -171,6 +171,22 @@ app.MapGet("/download", () => Results.File("myfile.text"));
171171

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

174+
### Modifying Headers
175+
176+
Use the `HttpResponse` object to modify response headers:
177+
178+
```csharp
179+
app.MapGet("/", (HttpContext context) => {
180+
// Set a custom header
181+
context.Response.Headers["X-Custom-Header"] = "CustomValue";
182+
183+
// Set a known header
184+
context.Response.Headers.CacheControl = $"public,max-age=3600";
185+
186+
return "Hello World";
187+
});
188+
```
189+
174190
### Customizing results
175191

176192
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:

aspnetcore/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public static void Main(string[] args)
4646
// <snippet_10>
4747
app.MapGet("/download", () => Results.File("myfile.text"));
4848
// </snippet_10>
49-
49+
// <snippet_12>
50+
app.MapGet("/problem", () =>
51+
{
52+
var extensions = new List<KeyValuePair<string, object?>> { new("test", "value") };
53+
return TypedResults.Problem("This is an error with extensions",
54+
extensions: extensions);
55+
});
56+
// </snippet_12>
5057
app.Run();
5158
}
5259
// <snippet_11>

aspnetcore/fundamentals/minimal-apis/responses.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: brunolins16
44
description: Learn how to create responses for minimal APIs in ASP.NET Core.
55
ms.author: brolivei
66
monikerRange: '>= aspnetcore-7.0'
7-
ms.date: 06/04/2024
7+
ms.date: 02/07/2025
88
uid: fundamentals/minimal-apis/responses
99
---
1010

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

147147
The preceding example returns a 500 status code.
148148

149+
#### Problem and ValidationProblem
150+
151+
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_12":::
152+
149153
#### Text
150154

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

196200
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).
197201

202+
## Modifying Headers
203+
204+
Use the `HttpResponse` object to modify response headers:
205+
206+
```csharp
207+
app.MapGet("/", (HttpContext context) => {
208+
// Set a custom header
209+
context.Response.Headers["X-Custom-Header"] = "CustomValue";
210+
211+
// Set a known header
212+
context.Response.Headers.CacheControl = $"public,max-age=3600";
213+
214+
return "Hello World";
215+
});
216+
```
217+
198218
## Customizing responses
199219

200220
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:

0 commit comments

Comments
 (0)