From 942c9069132f174595a2810830017ca863ab1505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 27 Jan 2025 13:46:56 +0100 Subject: [PATCH] Allow area as well. --- .../Extensions/HttpRequestExtensions.cs | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/Lombiq.HelpfulLibraries.AspNetCore/Extensions/HttpRequestExtensions.cs b/Lombiq.HelpfulLibraries.AspNetCore/Extensions/HttpRequestExtensions.cs index b96af203..19dd0112 100644 --- a/Lombiq.HelpfulLibraries.AspNetCore/Extensions/HttpRequestExtensions.cs +++ b/Lombiq.HelpfulLibraries.AspNetCore/Extensions/HttpRequestExtensions.cs @@ -67,46 +67,51 @@ public static string GetLinkAndCycleQueryValue(this HttpRequest request, string } /// - /// Checks if the and route values match the provided - /// arguments. + /// Checks if the , and route values + /// match the provided arguments. /// - public static bool IsAction(this HttpRequest request, string controller, string action) + public static bool IsAction(this HttpRequest request, string controller, string action, string area = null) { var values = request.RouteValues; return (string.IsNullOrEmpty(controller) || values.GetMaybe(nameof(controller))?.ToString().EqualsOrdinalIgnoreCase(controller) == true) && - (string.IsNullOrEmpty(action) || values.GetMaybe(nameof(action))?.ToString().EqualsOrdinalIgnoreCase(action) == true); + (string.IsNullOrEmpty(action) || values.GetMaybe(nameof(action))?.ToString().EqualsOrdinalIgnoreCase(action) == true) && + (string.IsNullOrEmpty(area) || values.GetMaybe(nameof(area))?.ToString().EqualsOrdinalIgnoreCase(area) == true); } /// - /// Checks if the controller and route values match the provided - /// arguments. + /// Checks if the , controller and route values match the + /// provided arguments. /// - public static bool IsAction(this HttpRequest request, string action) - where TController : Controller + public static bool IsAction(this HttpRequest request, string action, string area = null) + where TController : ControllerBase { var controllerType = typeof(TController); var controllerName = controllerType.Name.EndsWith(nameof(Controller), StringComparison.OrdinalIgnoreCase) ? controllerType.Name[..^nameof(Controller).Length] : controllerType.Name; - return request.IsAction(controllerName, action); + return request.IsAction(controllerName, action, area); } /// /// Checks if the controller and action route values match the . + /// Optionally, checks if the provided matches the route value of the same name as well. /// - public static bool IsAction(this HttpRequest request, Expression> actionSelector) - where TController : Controller + public static bool IsAction( + this HttpRequest request, + Expression> actionSelector, + string area = null) + where TController : ControllerBase { var action = actionSelector.GetMethodCallInfo().Method.Name; return request.IsAction(action); } - /// - public static bool IsAction(this HttpRequest request, Expression> actionSelector) - where TController : Controller - { - var action = actionSelector.GetMethodCallInfo().Method.Name; - return request.IsAction(action); - } + /// + public static bool IsAction( + this HttpRequest request, + Expression> actionSelector, + string area = null) + where TController : ControllerBase => + request.IsAction(actionSelector.StripResult(), area); }