Skip to content

fix NavLink with query string can't be selected correctly #32168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Components/Web/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Microsoft.AspNetCore.Components.RenderTree.WebEventDescriptor.EventName.get -> s
Microsoft.AspNetCore.Components.RenderTree.WebEventDescriptor.EventName.set -> void
*REMOVED*Microsoft.AspNetCore.Components.RenderTree.WebEventDescriptor.EventArgsType.get -> string!
*REMOVED*Microsoft.AspNetCore.Components.RenderTree.WebEventDescriptor.EventArgsType.set -> void
Microsoft.AspNetCore.Components.Routing.NavLink.IgnoreQueryString.get -> bool
Microsoft.AspNetCore.Components.Routing.NavLink.IgnoreQueryString.set -> void
15 changes: 13 additions & 2 deletions src/Components/Web/src/Routing/NavLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public class NavLink : ComponentBase, IDisposable
[Parameter]
public NavLinkMatch Match { get; set; }

/// <summary>
/// Gets or sets a flag to indicate whether route matching should ignore the query string.
/// </summary>
[Parameter]
public bool IgnoreQueryString { get; set; }

[Inject] private NavigationManager NavigationManager { get; set; } = default!;

/// <inheritdoc />
Expand Down Expand Up @@ -115,13 +121,15 @@ private bool ShouldMatch(string currentUriAbsolute)
return false;
}

if (EqualsHrefExactlyOrIfTrailingSlashAdded(currentUriAbsolute))
var matchingUri = IgnoreQueryString ? RemoveQueryString(currentUriAbsolute) : currentUriAbsolute;

if (EqualsHrefExactlyOrIfTrailingSlashAdded(matchingUri))
{
return true;
}

if (Match == NavLinkMatch.Prefix
&& IsStrictlyPrefixWithSeparator(currentUriAbsolute, _hrefAbsolute))
&& IsStrictlyPrefixWithSeparator(matchingUri, _hrefAbsolute))
{
return true;
}
Expand Down Expand Up @@ -173,6 +181,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
private string? CombineWithSpace(string? str1, string str2)
=> str1 == null ? str2 : $"{str1} {str2}";

private string RemoveQueryString(string path)
=> path.Split('?')[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be a little bit more defensive here can we check to see if the string contains a ? character first and then produce a substring only if it does?


private static bool IsStrictlyPrefixWithSeparator(string value, string prefix)
{
var prefixLength = prefix.Length;
Expand Down