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.
Top Level API Program
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment