Last active
July 3, 2023 16:46
-
-
Save layomia/d0da997f633310c2c6ee00fdca696371 to your computer and use it in GitHub Desktop.
ExtendedProgram.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.Json.Serialization; | |
using AotWebApi; | |
using Microsoft.Extensions.Options; | |
var builder = WebApplication.CreateSlimBuilder(args); | |
builder.Services.ConfigureHttpJsonOptions(options => | |
{ | |
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); | |
}); | |
/* Use configuration binding. */ | |
builder.Services | |
.AddOptions<AppSettings>() | |
.BindConfiguration(nameof(AppSettings)); | |
/* Use configuration binding.*/ | |
var app = builder.Build(); | |
var sampleTodos = TodoGenerator.GenerateTodos(); | |
var todosApi = app.MapGroup("/todos"); | |
/* Use configuration binding. */ | |
todosApi.MapGet("/", (IOptions<AppSettings> settings) => sampleTodos.Take(settings.Value.MaxReturnCount)); | |
/* Use configuration binding. */ | |
todosApi.MapGet("/{id}", (int id) => | |
sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo | |
? Results.Ok(todo) | |
: Results.NotFound()); | |
app.Run(); | |
[JsonSerializable(typeof(IEnumerable<Todo>))] | |
[JsonSerializable(typeof(AppSettings))] | |
internal partial class AppJsonSerializerContext : JsonSerializerContext | |
{ | |
} | |
/* Use configuration binding */ | |
internal class AppSettings | |
{ | |
public int MaxReturnCount { get; set; } | |
} | |
/* Use configuration binding */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment