public override void Process(TagHelperContext context, TagHelperOutput output) { if (ActiveWhen == null) { return; } var targetController = ActiveWhen.Split("/")[1]; var targetAction = ActiveWhen.Split("/")[2]; var currentController = ViewContextData.RouteData.Values["controller"].ToString(); var currentAction = ViewContextData.RouteData.Values["action"].ToString(); if (currentController.Equals(targetController) && currentAction.Equals(targetAction)) { if (output.Attributes.ContainsName("class")) { output.Attributes.SetAttribute("class", $"{output.Attributes["class"].Value} active"); } else { output.Attributes.SetAttribute("class", "active"); } } }
public override void Process(TagHelperContext context, TagHelperOutput output) { if (ActiveWhen == null) { return; } var targetController = ActiveWhen.Split("/")[1]; var targetAction = ActiveWhen.Split("/")[2]; var actions = new List <string>(); if (targetAction.Contains("|")) { actions.AddRange(targetAction.Split('|')); } else { actions.Add(targetAction); } var currentController = ViewContextData.RouteData.Values["controller"].ToString(); var currentAction = ViewContextData.RouteData.Values["action"].ToString(); if (!currentController.Equals(targetController)) { return; } if (string.IsNullOrEmpty(targetAction) || actions.Any(x => x.Equals(currentAction))) { if (output.Attributes.ContainsName("class")) { output.Attributes.SetAttribute("class", $"{output.Attributes["class"].Value} active"); } else { output.Attributes.SetAttribute("class", "active"); } } }
public override void Process(TagHelperContext context, TagHelperOutput output) { if (ActiveWhen == null) { return; } var targetController = ActiveWhen.Split("/")[1]; var targetAction = ActiveWhen.Split("/")[2]; var currentController = ViewContextData.RouteData.Values["controller"]?.ToString(); var currentAction = ViewContextData.RouteData.Values["action"]?.ToString(); var currentPage = ViewContextData.RouteData.Values["page"]?.ToString(); if (currentController != null && currentAction != null && currentController.Equals(targetController) && currentAction.Equals(targetAction)) { AddClass(output); } else if (currentPage != null && currentPage.Equals(ActiveWhen)) { AddClass(output); } }