Update Program.cs (#643)

* Update Program.cs

Use model binding

* Update Program.cs
This commit is contained in:
David Fowler 2022-05-06 13:27:40 -07:00 committed by GitHub
parent 57fc9655cb
commit b9b71c60a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 8 deletions

View File

@ -1,17 +1,18 @@
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
if (app.Environment.IsDevelopment()) {app.UseDeveloperExceptionPage();}
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapPost("/orders", async context => {
var data = await context.Request.ReadFromJsonAsync<Order>();
Console.WriteLine("Order received : " + data);
await context.Response.WriteAsync(data.ToString());
app.MapPost("/orders", (Order order) =>
{
Console.WriteLine("Order received : " + order);
return order.ToString();
});
await app.RunAsync();
public record Order([property: JsonPropertyName("orderId")] int orderId);
public record Order(int orderId);