|
1 | 1 | using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
2 | 2 | using Microsoft.Extensions.Diagnostics.HealthChecks; |
3 | 3 |
|
4 | | -using System.Text; |
5 | | - |
6 | 4 | using WebSampleApp; |
7 | 5 | using WebSampleApp.Extensions; |
8 | 6 | using WebSampleApp.Middleware; |
|
79 | 77 | endpoints.Map("sethealthy", async context => |
80 | 78 | { |
81 | 79 | var changeHealthService = context.RequestServices.GetRequiredService<HealthSample>(); |
82 | | - string healthyValue = context.Request.Query["healthy"]; |
| 80 | + string? healthyValue = context.Request.Query["healthy"]; |
83 | 81 | if (bool.TryParse(healthyValue, out bool healthy)) |
84 | 82 | { |
85 | 83 | changeHealthService.SetHealthy(healthy); |
|
96 | 94 | var service = context.RequestServices.GetRequiredService<RequestAndResponseSamples>(); |
97 | 95 | string? action = context.GetRouteValue("action")?.ToString(); |
98 | 96 | string method = context.Request.Method; |
99 | | - string result = (action, method) switch |
| 97 | + string? result = (action, method) switch |
100 | 98 | { |
101 | 99 | (null, "GET") => service.GetRequestInformation(context.Request), |
102 | 100 | ("header", "GET") => service.GetHeaderInformation(context.Request), |
|
108 | 106 | ("json", "GET") => service.GetJson(context.Response), |
109 | 107 | _ => string.Empty |
110 | 108 | }; |
| 109 | + // TODO: check with a newer compiler version if the previous variable can be declared non-nullable |
| 110 | + if (result is null) throw new InvalidOperationException("result should not be null with the previous operation"); |
111 | 111 |
|
112 | 112 | if (action is "json") |
113 | 113 | { |
|
135 | 135 |
|
136 | 136 | endpoints.MapGet("/", async context => |
137 | 137 | { |
138 | | - string[] lines = new[] |
139 | | - { |
140 | | - @"<ul>", |
141 | | - @"<li><a href=""/hello.html"">Static Files</a> - requires UseStaticFiles</li>", |
142 | | - @"<li><a href=""/add/37/5"">Route Constraints</a></li>", |
143 | | - @"<li>Request and Response", |
144 | | - @"<ul>", |
145 | | - @"<li><a href=""/randr"">Request and Response</a></li>", |
146 | | - @"<li><a href=""/randr/header"">Request headers</a></li>", |
147 | | - @"<li><a href=""/randr/add?x=38&y=4"">Add</a></li>", |
148 | | - @"<li><a href=""/randr/content?data=sample"">Content</a></li>", |
149 | | - @"<li><a href=""/randr/content?data=<h1>Heading 1</h1>"">HTML Content</a></li>", |
150 | | - @"<li><a href=""/randr/content?data=<script>alert('hacker');</script>"">Bad Content</a></li>", |
151 | | - @"<li><a href=""/randr/encoded?data=<h1>sample</h1>"">Encoded content</a></li>", |
152 | | - @"<li><a href=""/randr/encoded?data=<script>alert('hacker');</script>"">Encoded bad Content</a></li>", |
153 | | - @"<li><a href=""/randr/form"">Form</a></li>", |
154 | | - @"<li><a href=""/randr/writecookie"">Write cookie</a></li>", |
155 | | - @"<li><a href=""/randr/readcookie"">Read cookie</a></li>", |
156 | | - @"<li><a href=""/randr/json"">JSON</a></li>", |
157 | | - @"</ul>", |
158 | | - @"</li>", |
159 | | - @"<li><a href=""/session"">Session</a></li>", |
160 | | - @"<li>Health check", |
161 | | - @"<ul>", |
162 | | - @"<li><a href=""/health/live"">live</a></li>", |
163 | | - @"<li><a href=""/health/ready"">ready</a></li>", |
164 | | - @"<li><a href=""/health/allchecks"">all checks</a></li>", |
165 | | - @"<li><a href=""/sethealthy?healthy=true"">set healthy</a></li>", |
166 | | - @"<li><a href=""/sethealthy?healthy=false"">set unhealthy</a></li>", |
167 | | - @"</ul>", |
168 | | - @"</li>", |
169 | | - @"</ul>" |
170 | | - }; |
171 | | - |
172 | | - StringBuilder sb = new(); |
173 | | - foreach (var line in lines) |
174 | | - { |
175 | | - sb.Append(line); |
176 | | - } |
177 | | - string html = sb.ToString().HtmlDocument("Web Sample App"); |
| 138 | + string content = """ |
| 139 | + <ul> |
| 140 | + <li><a href="/hello.html">Static Files</a> - requires UseStaticFiles</li> |
| 141 | + <li><a href="/add/37/5">Route Constraints</a></li> |
| 142 | + <li>Request and Response |
| 143 | + <ul> |
| 144 | + <li><a href="/randr">Request and Response</a></li> |
| 145 | + <li><a href="/randr/header">Request headers</a></li> |
| 146 | + <li><a href="/randr/add?x=38&y=4">Add</a></li> |
| 147 | + <li><a href="/randr/content?data=sample">Content</a></li> |
| 148 | + <li><a href="/randr/content?data=<h1>Heading 1</h1>">HTML Content</a></li> |
| 149 | + <li><a href="/randr/content?data=<script>alert('hacker');</script>">Bad Content</a></li> |
| 150 | + <li><a href="/randr/encoded?data=<h1>sample</h1>">Encoded content</a></li> |
| 151 | + <li><a href="/randr/encoded?data=<script>alert('hacker');</script>">Encoded bad Content</a></li> |
| 152 | + <li><a href="/randr/form">Form</a></li> |
| 153 | + <li><a href="/randr/writecookie">Write cookie</a></li> |
| 154 | + <li><a href="/randr/readcookie">Read cookie</a></li> |
| 155 | + <li><a href="/randr/json">JSON</a></li> |
| 156 | + </ul> |
| 157 | + </li> |
| 158 | + <li><a href="/session">Session</a></li> |
| 159 | + <li>Health check |
| 160 | + <ul> |
| 161 | + <li><a href="/health/live">live</a></li> |
| 162 | + <li><a href="/health/ready">ready</a></li> |
| 163 | + <li><a href="/health/allchecks">all checks</a></li> |
| 164 | + <li><a href="/sethealthy?healthy=true">set healthy</a></li> |
| 165 | + <li><a href="/sethealthy?healthy=false">set unhealthy</a></li> |
| 166 | + </ul> |
| 167 | + </li> |
| 168 | + </ul> |
| 169 | + """; |
| 170 | + string html = content.HtmlDocument("Web Sample App"); |
178 | 171 |
|
179 | 172 | await context.Response.WriteAsync(html); |
180 | 173 | }); |
|
0 commit comments