Skip to content

Instantly share code, notes, and snippets.

@OldWarrior3000
Last active February 21, 2021 19:08
Show Gist options
  • Save OldWarrior3000/ec14a3bad9e633bd202a6590f66456db to your computer and use it in GitHub Desktop.
Save OldWarrior3000/ec14a3bad9e633bd202a6590f66456db to your computer and use it in GitHub Desktop.

Revisions

  1. OldWarrior3000 renamed this gist Feb 21, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. OldWarrior3000 renamed this gist Feb 21, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. OldWarrior3000 revised this gist Feb 21, 2021. No changes.
  4. OldWarrior3000 created this gist Feb 21, 2021.
    35 changes: 35 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.DependencyInjection;

    // with endpoint routing and JSON endpoints
    // see https://github.com/dotnet/aspnetcore/pull/23496/ for more on the JSON helpers
    WebHost.CreateDefaultBuilder().
    ConfigureServices(s => s.AddSingleton<ContactService>()).
    Configure(app =>
    {
    app.UseRouting();
    app.UseEndpoints(e =>
    {
    var contactService = e.ServiceProvider.GetRequiredService<ContactService>();

    e.MapGet("/contacts",
    async c => await c.Response.WriteAsJsonAsync(await contactService.GetAll()));
    e.MapGet("/contacts/{id:int}",
    async c => await c.Response.WriteAsJsonAsync(await contactService.Get(int.Parse((string)c.Request.RouteValues["id"]))));
    e.MapPost("/contacts",
    async c =>
    {
    await contactService.Add(await c.Request.ReadFromJsonAsync<Contact>());
    c.Response.StatusCode = 201;
    });
    e.MapDelete("/contacts/{id:int}",
    async c =>
    {
    await contactService.Delete(int.Parse((string)c.Request.RouteValues["id"]));
    c.Response.StatusCode = 204;
    });
    });
    }).Build().Run();