diff --git a/aspnetcore/fundamentals/minimal-apis.md b/aspnetcore/fundamentals/minimal-apis.md index 01603e980ba9..fd41b6342f22 100644 --- a/aspnetcore/fundamentals/minimal-apis.md +++ b/aspnetcore/fundamentals/minimal-apis.md @@ -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 --- @@ -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 type. The following code is an example of an HTML result type: diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs b/aspnetcore/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs index c75561bdcfd4..549ee577fdba 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs @@ -46,7 +46,14 @@ public static void Main(string[] args) // app.MapGet("/download", () => Results.File("myfile.text")); // - + // + app.MapGet("/problem", () => + { + var extensions = new List> { new("test", "value") }; + return TypedResults.Problem("This is an error with extensions", + extensions: extensions); + }); + // app.Run(); } // diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 0a7714c7ae59..08c13e268d3b 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -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 --- @@ -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"::: @@ -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 type. The following code is an example of an HTML result type: