cs11dotnet7/vscode/PracticalApps/Northwind.Web/Program.cs

70 lines
1.7 KiB
C#
Raw Normal View History

2022-03-15 12:01:57 +01:00
using Microsoft.AspNetCore.Server.Kestrel.Core; // HttpProtocols
2022-03-13 17:17:01 +01:00
using Packt.Shared; // AddNorthwindContext extension method
// configure services
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddNorthwindContext();
2022-03-15 12:01:57 +01:00
2022-09-24 10:10:26 +02:00
builder.Services.AddRequestDecompression();
/*
2022-03-15 12:01:57 +01:00
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.ListenAnyIP(5001, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
listenOptions.UseHttps(); // HTTP/3 requires secure connections
});
});
2022-09-24 10:10:26 +02:00
*/
2022-03-15 12:01:57 +01:00
2022-03-13 17:17:01 +01:00
var app = builder.Build();
// configure the HTTP pipeline
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.Use(async (HttpContext context, Func<Task> next) =>
{
RouteEndpoint? rep = context.GetEndpoint() as RouteEndpoint;
if (rep is not null)
{
WriteLine($"Endpoint name: {rep.DisplayName}");
WriteLine($"Endpoint route pattern: {rep.RoutePattern.RawText}");
}
if (context.Request.Path == "/bonjour")
{
// in the case of a match on URL path, this becomes a terminating
// delegate that returns so does not call the next delegate
await context.Response.WriteAsync("Bonjour Monde!");
return;
}
// we could modify the request before calling the next delegate
await next();
// we could modify the response after calling the next delegate
});
app.UseHttpsRedirection();
2022-09-24 10:10:26 +02:00
app.UseRequestDecompression();
2022-03-13 17:17:01 +01:00
app.UseDefaultFiles(); // index.html, default.html, and so on
app.UseStaticFiles();
app.MapRazorPages();
app.MapGet("/hello", () => "Hello World!");
// start the web server
app.Run();
WriteLine("This executes after the web server has stopped!");