3
3
using Umbraco . Cms . Core . Web ;
4
4
using Umbraco . Extensions ;
5
5
using Microsoft . AspNetCore . Mvc . TagHelpers ;
6
+ using System ;
6
7
7
8
namespace Our . Umbraco . TagHelpers
8
9
{
@@ -31,7 +32,7 @@ public IsActivePageTagHelper(IUmbracoContextAccessor umbracoContextAccessor)
31
32
/// </summary>
32
33
[ HtmlAttributeName ( tagHelperAttributeName ) ]
33
34
public string ActiveClassName { get ; set ; }
34
-
35
+
35
36
public override void Process ( TagHelperContext context , TagHelperOutput output )
36
37
{
37
38
// Remove the attribute
@@ -47,25 +48,26 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
47
48
return ;
48
49
}
49
50
50
- // Clean href values of any querystrings
51
- // As we won't be able to query GetByRoute for an Umbraco node with them
52
- href = href . Substring ( 0 , href . IndexOf ( "?" ) > 0 ? href . IndexOf ( "?" ) : href . Length ) ;
53
-
54
- // Get the node based of the value in the HREF
55
- var nodeOfLink = ctx . Content . GetByRoute ( href ) ;
56
- if ( nodeOfLink == null )
51
+ // Try & parse href as URI, as it could be relative or absolute
52
+ // or contain a quersystring we only want the path part
53
+ if ( Uri . TryCreate ( href , UriKind . Absolute , out Uri link ) || Uri . TryCreate ( ctx . PublishedRequest . Uri , href , out link ) )
57
54
{
58
- return ;
59
- }
55
+ // Get the node based of the value in the HREF
56
+ var nodeOfLink = ctx . Content . GetByRoute ( link . AbsolutePath ) ;
57
+ if ( nodeOfLink == null )
58
+ {
59
+ return ;
60
+ }
60
61
61
- // Get the current node of the page that is rendering
62
- var currentPageRendering = ctx . PublishedRequest . PublishedContent ;
62
+ // Get the current node of the page that is rendering
63
+ var currentPageRendering = ctx . PublishedRequest . PublishedContent ;
63
64
64
- // Check if thelink we are rendering is current page or an ancestor
65
- if ( nodeOfLink . IsAncestorOrSelf ( currentPageRendering ) )
66
- {
67
- // Is active page
68
- output . AddClass ( ActiveClassName , HtmlEncoder . Default ) ;
65
+ // Check if thelink we are rendering is current page or an ancestor
66
+ if ( nodeOfLink . IsAncestorOrSelf ( currentPageRendering ) )
67
+ {
68
+ // Is active page
69
+ output . AddClass ( ActiveClassName , HtmlEncoder . Default ) ;
70
+ }
69
71
}
70
72
}
71
73
}
0 commit comments