Skip to content

Commit ff043d0

Browse files
committed
ReasonPhrases as switch instead of Dictionary
1 parent c60c987 commit ff043d0

File tree

1 file changed

+72
-74
lines changed

1 file changed

+72
-74
lines changed

src/Http/WebUtilities/src/ReasonPhrases.cs

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,83 @@ namespace Microsoft.AspNetCore.WebUtilities;
99
/// </summary>
1010
public static class ReasonPhrases
1111
{
12-
// Status Codes listed at http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
13-
private static readonly Dictionary<int, string> Phrases = new()
14-
{
15-
{ 100, "Continue" },
16-
{ 101, "Switching Protocols" },
17-
{ 102, "Processing" },
18-
19-
{ 200, "OK" },
20-
{ 201, "Created" },
21-
{ 202, "Accepted" },
22-
{ 203, "Non-Authoritative Information" },
23-
{ 204, "No Content" },
24-
{ 205, "Reset Content" },
25-
{ 206, "Partial Content" },
26-
{ 207, "Multi-Status" },
27-
{ 208, "Already Reported" },
28-
{ 226, "IM Used" },
29-
30-
{ 300, "Multiple Choices" },
31-
{ 301, "Moved Permanently" },
32-
{ 302, "Found" },
33-
{ 303, "See Other" },
34-
{ 304, "Not Modified" },
35-
{ 305, "Use Proxy" },
36-
{ 306, "Switch Proxy" },
37-
{ 307, "Temporary Redirect" },
38-
{ 308, "Permanent Redirect" },
39-
40-
{ 400, "Bad Request" },
41-
{ 401, "Unauthorized" },
42-
{ 402, "Payment Required" },
43-
{ 403, "Forbidden" },
44-
{ 404, "Not Found" },
45-
{ 405, "Method Not Allowed" },
46-
{ 406, "Not Acceptable" },
47-
{ 407, "Proxy Authentication Required" },
48-
{ 408, "Request Timeout" },
49-
{ 409, "Conflict" },
50-
{ 410, "Gone" },
51-
{ 411, "Length Required" },
52-
{ 412, "Precondition Failed" },
53-
{ 413, "Payload Too Large" },
54-
{ 414, "URI Too Long" },
55-
{ 415, "Unsupported Media Type" },
56-
{ 416, "Range Not Satisfiable" },
57-
{ 417, "Expectation Failed" },
58-
{ 418, "I'm a teapot" },
59-
{ 419, "Authentication Timeout" },
60-
{ 421, "Misdirected Request" },
61-
{ 422, "Unprocessable Entity" },
62-
{ 423, "Locked" },
63-
{ 424, "Failed Dependency" },
64-
{ 426, "Upgrade Required" },
65-
{ 428, "Precondition Required" },
66-
{ 429, "Too Many Requests" },
67-
{ 431, "Request Header Fields Too Large" },
68-
{ 451, "Unavailable For Legal Reasons" },
69-
{ 499, "Client Closed Request" },
70-
71-
{ 500, "Internal Server Error" },
72-
{ 501, "Not Implemented" },
73-
{ 502, "Bad Gateway" },
74-
{ 503, "Service Unavailable" },
75-
{ 504, "Gateway Timeout" },
76-
{ 505, "HTTP Version Not Supported" },
77-
{ 506, "Variant Also Negotiates" },
78-
{ 507, "Insufficient Storage" },
79-
{ 508, "Loop Detected" },
80-
{ 510, "Not Extended" },
81-
{ 511, "Network Authentication Required" },
82-
};
8312

8413
/// <summary>
8514
/// Gets the reason phrase for the specified status code.
8615
/// </summary>
8716
/// <param name="statusCode">The status code.</param>
8817
/// <returns>The reason phrase, or <see cref="string.Empty"/> if the status code is unknown.</returns>
89-
public static string GetReasonPhrase(int statusCode)
18+
public static string GetReasonPhrase(int statusCode) => statusCode switch
9019
{
91-
return Phrases.TryGetValue(statusCode, out var phrase) ? phrase : string.Empty;
92-
}
20+
// Status Codes listed at http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
21+
100 => "Continue",
22+
101 => "Switching Protocols",
23+
102 => "Processing",
24+
25+
200 => "OK",
26+
201 => "Created",
27+
202 => "Accepted",
28+
203 => "Non-Authoritative Information",
29+
204 => "No Content",
30+
205 => "Reset Content",
31+
206 => "Partial Content",
32+
207 => "Multi-Status",
33+
208 => "Already Reported",
34+
226 => "IM Used",
35+
36+
300 => "Multiple Choices",
37+
301 => "Moved Permanently",
38+
302 => "Found",
39+
303 => "See Other",
40+
304 => "Not Modified",
41+
305 => "Use Proxy",
42+
306 => "Switch Proxy",
43+
307 => "Temporary Redirect",
44+
308 => "Permanent Redirect",
45+
46+
400 => "Bad Request",
47+
401 => "Unauthorized",
48+
402 => "Payment Required",
49+
403 => "Forbidden",
50+
404 => "Not Found",
51+
405 => "Method Not Allowed",
52+
406 => "Not Acceptable",
53+
407 => "Proxy Authentication Required",
54+
408 => "Request Timeout",
55+
409 => "Conflict",
56+
410 => "Gone",
57+
411 => "Length Required",
58+
412 => "Precondition Failed",
59+
413 => "Payload Too Large",
60+
414 => "URI Too Long",
61+
415 => "Unsupported Media Type",
62+
416 => "Range Not Satisfiable",
63+
417 => "Expectation Failed",
64+
418 => "I'm a teapot",
65+
419 => "Authentication Timeout",
66+
421 => "Misdirected Request",
67+
422 => "Unprocessable Entity",
68+
423 => "Locked",
69+
424 => "Failed Dependency",
70+
426 => "Upgrade Required",
71+
428 => "Precondition Required",
72+
429 => "Too Many Requests",
73+
431 => "Request Header Fields Too Large",
74+
451 => "Unavailable For Legal Reasons",
75+
499 => "Client Closed Request",
76+
77+
500 => "Internal Server Error",
78+
501 => "Not Implemented",
79+
502 => "Bad Gateway",
80+
503 => "Service Unavailable",
81+
504 => "Gateway Timeout",
82+
505 => "HTTP Version Not Supported",
83+
506 => "Variant Also Negotiates",
84+
507 => "Insufficient Storage",
85+
508 => "Loop Detected",
86+
510 => "Not Extended",
87+
511 => "Network Authentication Required",
88+
89+
_ => string.Empty
90+
};
9391
}

0 commit comments

Comments
 (0)