-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Ignore query/fragment in ShouldMatch
of NavLink
by default but allow overriding ShouldMatch
#59903
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
825ea4c
Fix that is missing change to docs.
ilonatommy 5a6caeb
Revert previous approach, allow custom ShouldMatch implementations.
ilonatommy eecf253
Fix: Missing XML comment for publicly visible type or member.
ilonatommy 882e494
Revert unrelated test.
ilonatommy eda0dd0
Fix test with fragments - redirection has to be made from an orgin th…
ilonatommy 46e9c36
Breaking change: NavLinkMatch.All matches queries and fragments with …
ilonatommy de2ee78
Fix test: the order of links matters.
ilonatommy 863ce4a
App context switch to disable the update.
ilonatommy 181047c
Feedback.
ilonatommy 17ece19
Merge remote-tracking branch 'origin/main' into fix-31312
ilonatommy 0c7f19a
Feedback - avoid allocating a string.
ilonatommy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
virtual Microsoft.AspNetCore.Components.Routing.NavLink.ShouldMatch(string! currentUriAbsolute) -> bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Components/test/testassets/BasicTestApp/RouterTest/LayoutOverridden.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@page "/layout-overridden" | ||
@page "/layout-overridden/for-hash" | ||
@layout RouterTestLayoutNavLinksOverridden | ||
<div id="test-info">This is the page with overridden layout.</div> |
18 changes: 18 additions & 0 deletions
18
src/Components/test/testassets/BasicTestApp/RouterTest/LinksOverridden.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@using Microsoft.AspNetCore.Components.Routing | ||
<style type="text/css"> | ||
a.active { | ||
background-color: yellow; | ||
font-weight: bold; | ||
} | ||
</style> | ||
<ul> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden/" Match=NavLinkMatch.All>Override layout (matches all)</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden" Match=NavLinkMatch.All>Override layout, no trailing slash (matches all)</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden/?abc=123">Override layout with query</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden?abc=123">Override layout with query, no trailing slash</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden/#blah">Override layout with hash</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden#blah">Override layout with hash, no trailing slash</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden/Default.html">Override layout with extension</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/layout-overridden/Other">Override Other</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
<li><NavLinkNotIgnoreQueryOrFragmentString href="/subdir/Other" Match=NavLinkMatch.All>Override Other with base-relative URL (matches all)</NavLinkNotIgnoreQueryOrFragmentString></li> | ||
</ul> |
45 changes: 45 additions & 0 deletions
45
...mponents/test/testassets/BasicTestApp/RouterTest/NavLinkNotIgnoreQueryOrFragmentString.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Routing; | ||
|
||
public class NavLinkNotIgnoreQueryOrFragmentString : NavLink | ||
{ | ||
string hrefAbsolute; | ||
NavigationManager _navigationManager; | ||
|
||
public NavLinkNotIgnoreQueryOrFragmentString(NavigationManager navigationManager) | ||
{ | ||
_navigationManager = navigationManager; | ||
} | ||
|
||
protected override void OnInitialized() | ||
{ | ||
string href = ""; | ||
if (AdditionalAttributes != null && AdditionalAttributes.TryGetValue("href", out var obj)) | ||
{ | ||
href = Convert.ToString(obj, CultureInfo.InvariantCulture) ?? ""; | ||
} | ||
hrefAbsolute = _navigationManager.ToAbsoluteUri(href).AbsoluteUri; | ||
base.OnInitialized(); | ||
} | ||
protected override bool ShouldMatch(string currentUriAbsolute) | ||
{ | ||
bool baseMatch = base.ShouldMatch(currentUriAbsolute); | ||
if (!baseMatch || string.IsNullOrEmpty(hrefAbsolute) || Match != NavLinkMatch.All) | ||
{ | ||
return baseMatch; | ||
} | ||
|
||
if (NormalizeUri(hrefAbsolute) == NormalizeUri(currentUriAbsolute)) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static string NormalizeUri(string uri) => | ||
uri.EndsWith('/') ? uri.TrimEnd('/') : uri; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Components/test/testassets/BasicTestApp/RouterTestLayoutNavLinksOverridden.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@using Microsoft.AspNetCore.Components | ||
@inherits LayoutComponentBase | ||
|
||
@Body | ||
|
||
<BasicTestApp.RouterTest.LinksOverridden /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.