namespace marlin.web.TagHelpers; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; [HtmlTargetElement("a", Attributes = "active-when")] public class ActiveLinkTagHelper : TagHelper { [HtmlAttributeName("asp-controller")] public string Controller { get; set; } [HtmlAttributeName("asp-action")] public string Action { get; set; } [HtmlAttributeName("active-when")] public ActiveLinkWhen ActiveWhen { get; set; } [ViewContext] [HtmlAttributeNotBound] public ViewContext ViewContext { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { var currentController = ViewContext.RouteData.Values["controller"]?.ToString(); var currentAction = ViewContext.RouteData.Values["action"]?.ToString(); if (!string.Equals(currentController, Controller, StringComparison.OrdinalIgnoreCase)) return; if (!string.Equals(currentAction, Action, StringComparison.OrdinalIgnoreCase) && this.ActiveWhen == ActiveLinkWhen.ControllerAndAction) return; var existingClasses = output.Attributes["class"]?.Value?.ToString() ?? ""; output.Attributes.SetAttribute("class", $"{existingClasses} active".Trim()); } } public enum ActiveLinkWhen { Controller, ControllerAndAction }