From 3e4504bd6928e936b207b624f8603c0b26b0783e Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 20 Jun 2023 16:09:58 -1000 Subject: [PATCH] Support for AsParameters /2 (#29585) * generics in What's new /2 * generics in What's new /2 --- aspnetcore/release-notes/aspnetcore-8.0.md | 13 ++++++ .../sample/ProgramAsParameters.cs | 40 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 aspnetcore/release-notes/sample/ProgramAsParameters.cs diff --git a/aspnetcore/release-notes/aspnetcore-8.0.md b/aspnetcore/release-notes/aspnetcore-8.0.md index 2704471913..77ba82102e 100644 --- a/aspnetcore/release-notes/aspnetcore-8.0.md +++ b/aspnetcore/release-notes/aspnetcore-8.0.md @@ -160,6 +160,19 @@ The main entry points to subsystems that don't work reliably with native AOT are :::image type="content" source="../fundamentals/aot/_static/top-level-annnotations.png" alt-text="Visual Studio window showing IL2026 warning message on the AddControllers method that says MVC doesn't currently support native AOT."::: +### Support for AsParameters and automatic metadata generation + +In this preview, minimal APIs generated at compile-time include support for parameters decorated with the [`[AsParameters]`](xref:Microsoft.AspNetCore.Http.AsParametersAttribute) attribute and support automatic metadata inference for request and response types. Consider the following code: + +:::code language="csharp" source="~/release-notes/sample/ProgramAsParameters.cs" highlight="22"::: + +The preceding generated code: + +* Binds a `projectId` parameter from the query. +* Binds a `Todo` parameter from the JSON body. +* Annotates the endpoint metadata to indicate that it accepts a JSON payload. +* Annotate the endpoint metadata to indicate that it returns a Todo as a JSON payload. + ## Miscellaneous ### HTTP/3 enabled by default diff --git a/aspnetcore/release-notes/sample/ProgramAsParameters.cs b/aspnetcore/release-notes/sample/ProgramAsParameters.cs new file mode 100644 index 0000000000..251a761b71 --- /dev/null +++ b/aspnetcore/release-notes/sample/ProgramAsParameters.cs @@ -0,0 +1,40 @@ +using System.Text.Json.Serialization; +using MyFirstAotWebApi; + +var builder = WebApplication.CreateSlimBuilder(args); + +builder.Services.ConfigureHttpJsonOptions(options => +{ + options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); +}); + +var app = builder.Build(); + +var sampleTodos = TodoGenerator.GenerateTodos().ToArray(); + +var todosApi = app.MapGroup("/todos"); +todosApi.MapGet("/", () => sampleTodos); +todosApi.MapGet("/{id}", (int id) => + sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo + ? Results.Ok(todo) + : Results.NotFound()); + +app.MapPost("/todos", ([AsParameters] CreateTodoArgs payload) => +{ + if (payload.TodoToCreate is not null) + { + return payload.TodoToCreate; + } + return new Todo(0, "New todo", DateTime.Now, false); +}); + +app.Run(); + +[JsonSerializable(typeof(Todo[]))] +internal partial class AppJsonSerializerContext : JsonSerializerContext +{ + +} + +record CreateTodoArgs(int ProjectId, Todo? TodoToCreate); +record Todo(int Id, string Name, DateTime CreatedAt, bool IsCompleted); \ No newline at end of file