Skip to content

Commit fd0f269

Browse files
committed
Add documentation for modifying response headers in Minimal APIs
1 parent daad351 commit fd0f269

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

aspnetcore/fundamentals/minimal-apis.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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/responses.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ Here's an example of a filter that uses one of these interfaces:
195195

196196
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).
197197

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

200216
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)