Skip to content

Instantly share code, notes, and snippets.

@DanElliott
Forked from bjcull/ActiveRouteTagHelper.cs
Last active July 13, 2023 21:10
Show Gist options
  • Select an option

  • Save DanElliott/32787b4ae1941780d70cb085d55f8b24 to your computer and use it in GitHub Desktop.

Select an option

Save DanElliott/32787b4ae1941780d70cb085d55f8b24 to your computer and use it in GitHub Desktop.

Revisions

  1. DanElliott revised this gist Nov 17, 2017. No changes.
  2. DanElliott revised this gist Nov 17, 2017. 2 changed files with 55 additions and 93 deletions.
    55 changes: 55 additions & 0 deletions ActivePageTagHelper.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    [HtmlTargetElement(Attributes = "is-active-page")]
    public class ActivePageTagHelper : TagHelper
    {
    /// <summary>The name of the action method.</summary>
    /// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
    [HtmlAttributeName("asp-page")]
    public string Page { get; set; }

    /// <summary>
    /// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
    /// </summary>
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
    base.Process(context, output);

    if (ShouldBeActive())
    {
    MakeActive(output);
    }

    output.Attributes.RemoveAll("is-active-page");
    }

    private bool ShouldBeActive()
    {
    string currentPage = ViewContext.RouteData.Values["Page"].ToString();

    if (!string.IsNullOrWhiteSpace(Page) && Page.ToLower() != currentPage.ToLower())
    {
    return false;
    }

    return true;
    }

    private void MakeActive(TagHelperOutput output)
    {
    var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
    if (classAttr == null)
    {
    classAttr = new TagHelperAttribute("class", "active");
    output.Attributes.Add(classAttr);
    }
    else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
    {
    output.Attributes.SetAttribute("class", classAttr.Value == null
    ? "active"
    : classAttr.Value.ToString() + " active");
    }
    }
    }
    93 changes: 0 additions & 93 deletions ActiveRouteTagHelper.cs
    Original file line number Diff line number Diff line change
    @@ -1,93 +0,0 @@
    [HtmlTargetElement(Attributes = "is-active-route")]
    public class ActiveRouteTagHelper : TagHelper
    {
    private IDictionary<string, string> _routeValues;

    /// <summary>The name of the action method.</summary>
    /// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
    [HtmlAttributeName("asp-action")]
    public string Action { get; set; }

    /// <summary>The name of the controller.</summary>
    /// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
    [HtmlAttributeName("asp-controller")]
    public string Controller { get; set; }

    /// <summary>Additional parameters for the route.</summary>
    [HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")]
    public IDictionary<string, string> RouteValues
    {
    get
    {
    if (this._routeValues == null)
    this._routeValues = (IDictionary<string, string>)new Dictionary<string, string>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
    return this._routeValues;
    }
    set
    {
    this._routeValues = value;
    }
    }

    /// <summary>
    /// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
    /// </summary>
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
    base.Process(context, output);

    if (ShouldBeActive())
    {
    MakeActive(output);
    }

    output.Attributes.RemoveAll("is-active-route");
    }

    private bool ShouldBeActive()
    {
    string currentController = ViewContext.RouteData.Values["Controller"].ToString();
    string currentAction = ViewContext.RouteData.Values["Action"].ToString();

    if (!string.IsNullOrWhiteSpace(Controller) && Controller.ToLower() != currentController.ToLower())
    {
    return false;
    }

    if (!string.IsNullOrWhiteSpace(Action) && Action.ToLower() != currentAction.ToLower())
    {
    return false;
    }

    foreach (KeyValuePair<string, string> routeValue in RouteValues)
    {
    if (!ViewContext.RouteData.Values.ContainsKey(routeValue.Key) ||
    ViewContext.RouteData.Values[routeValue.Key].ToString() != routeValue.Value)
    {
    return false;
    }
    }

    return true;
    }

    private void MakeActive(TagHelperOutput output)
    {
    var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
    if (classAttr == null)
    {
    classAttr = new TagHelperAttribute("class", "active");
    output.Attributes.Add(classAttr);
    }
    else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
    {
    output.Attributes.SetAttribute("class", classAttr.Value == null
    ? "active"
    : classAttr.Value.ToString() + " active");
    }
    }
    }
  3. @bjcull bjcull revised this gist Apr 28, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ActiveRouteTagHelper.cs
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,8 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
    {
    MakeActive(output);
    }

    output.Attributes.RemoveAll("is-active-route");
    }

    private bool ShouldBeActive()
  4. @invalid-email-address Anonymous created this gist Jan 29, 2017.
    91 changes: 91 additions & 0 deletions ActiveRouteTagHelper.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    [HtmlTargetElement(Attributes = "is-active-route")]
    public class ActiveRouteTagHelper : TagHelper
    {
    private IDictionary<string, string> _routeValues;

    /// <summary>The name of the action method.</summary>
    /// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
    [HtmlAttributeName("asp-action")]
    public string Action { get; set; }

    /// <summary>The name of the controller.</summary>
    /// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
    [HtmlAttributeName("asp-controller")]
    public string Controller { get; set; }

    /// <summary>Additional parameters for the route.</summary>
    [HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")]
    public IDictionary<string, string> RouteValues
    {
    get
    {
    if (this._routeValues == null)
    this._routeValues = (IDictionary<string, string>)new Dictionary<string, string>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
    return this._routeValues;
    }
    set
    {
    this._routeValues = value;
    }
    }

    /// <summary>
    /// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
    /// </summary>
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
    base.Process(context, output);

    if (ShouldBeActive())
    {
    MakeActive(output);
    }
    }

    private bool ShouldBeActive()
    {
    string currentController = ViewContext.RouteData.Values["Controller"].ToString();
    string currentAction = ViewContext.RouteData.Values["Action"].ToString();

    if (!string.IsNullOrWhiteSpace(Controller) && Controller.ToLower() != currentController.ToLower())
    {
    return false;
    }

    if (!string.IsNullOrWhiteSpace(Action) && Action.ToLower() != currentAction.ToLower())
    {
    return false;
    }

    foreach (KeyValuePair<string, string> routeValue in RouteValues)
    {
    if (!ViewContext.RouteData.Values.ContainsKey(routeValue.Key) ||
    ViewContext.RouteData.Values[routeValue.Key].ToString() != routeValue.Value)
    {
    return false;
    }
    }

    return true;
    }

    private void MakeActive(TagHelperOutput output)
    {
    var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
    if (classAttr == null)
    {
    classAttr = new TagHelperAttribute("class", "active");
    output.Attributes.Add(classAttr);
    }
    else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
    {
    output.Attributes.SetAttribute("class", classAttr.Value == null
    ? "active"
    : classAttr.Value.ToString() + " active");
    }
    }
    }