public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); }) .ConfigureLogging((hostingContext, l) => { l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); l.AddConsole(); }) .UseIISIntegration() .ConfigureServices(s => s.AddRouting()) .Configure(app => { // define all API endpoints app.UseRouter(r => { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); response.WriteJson(contacts); }); }); }) .Build(); host.Run(); }
static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables().Build(); var host = new WebHostBuilder() .UseKestrel(k => k.Listen(System.Net.IPAddress.Any, 88)) .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureLogging(l => { l.AddConfiguration(config.GetSection("Logging")); l.AddConsole(); }) .ConfigureServices(s => s.AddRouting()) .Configure(app => { // define all API endpoints app.UseRouter(r => { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); await response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } await response.WriteJson(contact); }); }); }) .Build(); host.Run();; }
public static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables().Build(); var host = new WebHostBuilder() .UseKestrel() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .ConfigureLogging(l => l.AddConsole(config.GetSection("Logging"))) .ConfigureServices(s => s.AddRouting()) .Configure(app => { // define all API endpoints app.UseRouter(r => { var contactRepo = new InMemoryContactRepository(); r.MapGet("contacts", async(request, response, routeData) => { var contacts = await contactRepo.GetAll(); await response.WriteJson(contacts); }); r.MapGet("contacts/{id:int}", async(request, response, routeData) => { var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); if (contact == null) { response.StatusCode = 404; return; } await response.WriteJson(contact); }); r.MapPost("contacts", async(request, response, routeData) => { var newContact = await request.HttpContext.ReadFromJson <Contact>(); if (newContact == null) { return; } await contactRepo.Add(newContact); response.StatusCode = 201; await response.WriteJson(newContact); }); r.MapPut("contacts/{id:int}", async(request, response, routeData) => { var updatedContact = await request.HttpContext.ReadFromJson <Contact>(); if (updatedContact == null) { return; } updatedContact.ContactId = Convert.ToInt32(routeData.Values["id"]); await contactRepo.Update(updatedContact); response.StatusCode = 204; }); r.MapDelete("contacts/{id:int}", async(request, response, routeData) => { await contactRepo.Delete(Convert.ToInt32(routeData.Values["id"])); response.StatusCode = 204; }); }); }) .Build(); host.Run(); }