-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore query/fragment in
ShouldMatch
of NavLink
by default but al…
…low overriding `ShouldMatch` (#59903) * NavLinkMatch.All matches queries and fragments with an option to override this behavior. * App context switch to disable the update.
- Loading branch information
1 parent
4bb5e98
commit d498fbc
Showing
7 changed files
with
192 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 /> |