using System.Net; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Primitives; namespace Nop.Core.Http.Extensions; /// /// HttpRequest extensions /// public static class HttpRequestExtensions { /// /// Check if the request is the POST request /// /// Request to check /// True if the request is POST request, false in all other cases public static bool IsPostRequest(this HttpRequest request) { return request.Method.Equals(WebRequestMethods.Http.Post, StringComparison.InvariantCultureIgnoreCase); } /// /// Check if the request is the GET request /// /// Request to check /// True if the request is GET request, false in all other cases public static bool IsGetRequest(this HttpRequest request) { return request.Method.Equals(WebRequestMethods.Http.Get, StringComparison.InvariantCultureIgnoreCase); } /// /// Gets the form value /// /// Request /// The form key /// /// A task that represents the asynchronous operation /// The task result contains the form value /// public static async Task GetFormValueAsync(this HttpRequest request, string formKey) { if (!request.HasFormContentType) return new StringValues(); var form = await request.ReadFormAsync(); return form[formKey]; } /// /// Checks if the provided key is exists on the form /// /// Request /// Form key /// /// A task that represents the asynchronous operation /// The task result contains true if the key is persists in the form, false in other case /// public static async Task IsFormKeyExistsAsync(this HttpRequest request, string formKey) { return await IsFormAnyAsync(request, key => key.Equals(formKey)); } /// /// Checks if the key is exists on the form /// /// Request /// Filter. Set null if filtering no need /// /// A task that represents the asynchronous operation /// The task result contains true if the any item is persists in the form, false in other case /// public static async Task IsFormAnyAsync(this HttpRequest request, Func predicate = null) { if (!request.HasFormContentType) return false; var form = await request.ReadFormAsync(); return predicate == null ? form.Any() : form.Keys.Any(predicate); } /// /// Gets the value associated with the specified form key /// /// Request /// The form key /// /// A task that represents the asynchronous operation /// The task result contains true and the form value if the form contains an element with the specified key; otherwise, false and default value. /// public static async Task<(bool keyExists, StringValues formValue)> TryGetFormValueAsync(this HttpRequest request, string formKey) { if (!request.HasFormContentType) return (false, default); var form = await request.ReadFormAsync(); var flag = form.TryGetValue(formKey, out var formValue); return (flag, formValue); } /// /// Returns the first element of the Form.Files, or a default value if the sequence contains no elements /// /// Request /// /// A task that represents the asynchronous operation /// The task result contains the element or default value /// public static async Task GetFirstOrDefaultFileAsync(this HttpRequest request) { if (!request.HasFormContentType) return default; var form = await request.ReadFormAsync(); return form.Files.FirstOrDefault(); } }