-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathProgram.cs
73 lines (54 loc) · 1.95 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Microsoft.AspNetCore.Server.Kestrel.Core; // To use HttpProtocols.
using Northwind.EntityModels; // To use AddNorthwindContext method.
#region Configure the web server host and services
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddNorthwindContext();
builder.Services.AddRequestDecompression();
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
listenOptions.UseHttps(); // HTTP/3 requires secure connections.
});
});
var app = builder.Build();
#endregion
#region Configure the HTTP pipeline and routes
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseRequestDecompression();
// Implementing an anonymous inline delegate as middleware
// to intercept HTTP requests and responses.
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();
app.UseDefaultFiles(); // index.html, default.html, and so on.
app.UseStaticFiles();
app.MapRazorPages();
app.MapGet("/hello", () =>
$"Environment is {app.Environment.EnvironmentName}");
#endregion
// Start the web server, host the website, and wait for requests.
app.Run(); // This is a thread-blocking call.
WriteLine("This executes after the web server has stopped!");