-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapital.cs
More file actions
83 lines (74 loc) · 2.21 KB
/
Capital.cs
File metadata and controls
83 lines (74 loc) · 2.21 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 Capital
{
private RequestDelegate? next;
public Capital()
{
}
public Capital(RequestDelegate next)
{
this.next = next;
}
public static async Task Endpoint(HttpContext context)
{
string? capital = null;
string? country = context.Request.RouteValues["country"] as string;
switch ((country ?? "").ToLower())
{
case "uk":
capital = "London";
break;
case "france":
capital = "Paris";
break;
case "monaco":
LinkGenerator? generator =
context.RequestServices.GetService<LinkGenerator>();
string? url = generator?.GetPathByRouteValues(context,
"population", new {city = country});
if (url != null)
{
context.Response.Redirect(url);
}
return;
}
if (capital != null)
{
await context.Response.WriteAsync($"{capital} is the capital of {country}");
}
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] == "capital")
{
string? capital = null;
string country = parts[1];
switch (country.ToLower())
{
case "uk":
capital = "London";
break;
case "france":
capital = "Paris";
break;
case "monaco":
context.Response.Redirect($"/population/country/{country}");
return;
}
if (capital != null)
{
await context.Response.WriteAsync($"{capital} is the capital of {country}");
return;
}
}
if (next != null)
{
await next(context);
}
}
}