-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopulation.cs
More file actions
83 lines (72 loc) · 2.04 KB
/
Population.cs
File metadata and controls
83 lines (72 loc) · 2.04 KB
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
74
75
76
77
78
79
80
81
82
83
namespace Platform;
public class Population
{
private RequestDelegate? next;
public Population() { }
public Population(RequestDelegate nextDelegate)
{
next = nextDelegate;
}
public static async Task NewEndpoint(HttpContext context)
{
foreach (var item in context.Request.RouteValues)
{
await context.Response.WriteAsync($"Key: {item.Key}, Value: {item.Value}\n");
}
}
public static async Task Endpoint(HttpContext context)
{
string? city = context.Request.RouteValues["city"] as string;
int? pop = null;
switch ((city ?? "").ToLower())
{
case "london":
pop = 8_136_000;
break;
case "paris":
pop = 2_141_000;
break;
case "monaco":
pop = 39_000;
break;
}
if (pop.HasValue)
{
await context.Response.WriteAsync($"City: {city}, Population: {pop}");
}
else
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
}
}
public async Task Invoke(HttpContext context)
{
string[] parts = context.Request.Path.ToString().Split("/", StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2 && parts[0] == "population")
{
string city = parts[1];
int? pop = null;
switch (city.ToLower())
{
case "london":
pop = 8_136_000;
break;
case "paris":
pop = 2_141_000;
break;
case "monaco":
pop = 39_000;
break;
}
if (pop.HasValue)
{
await context.Response.WriteAsync($"City: {city}, Population: {pop}");
return;
}
}
if (next != null)
{
await next(context);
}
}
}