Replaced the switch statement in the ToHttpMethod method with a case-insensitive dictionary for mapping HTTP method strings to HttpMethod objects. Introduced a private static dictionary to store predefined HTTP methods, including the addition of the CONNECT method. Improved performance and maintainability by leveraging dictionary lookups for faster and cleaner code.
22 lines
709 B
C#
22 lines
709 B
C#
namespace ReC.Application.Common;
|
|
|
|
public static class HttpExtensions
|
|
{
|
|
private static readonly Dictionary<string, HttpMethod> _methods = new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
["GET"] = HttpMethod.Get,
|
|
["POST"] = HttpMethod.Post,
|
|
["PUT"] = HttpMethod.Put,
|
|
["DELETE"] = HttpMethod.Delete,
|
|
["PATCH"] = HttpMethod.Patch,
|
|
["HEAD"] = HttpMethod.Head,
|
|
["OPTIONS"] = HttpMethod.Options,
|
|
["TRACE"] = HttpMethod.Trace,
|
|
["CONNECT"] = HttpMethod.Connect
|
|
};
|
|
|
|
public static HttpMethod ToHttpMethod(this string method) => _methods.TryGetValue(method, out var httpMethod)
|
|
? httpMethod
|
|
: new HttpMethod(method);
|
|
}
|