Skip to content
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

Added additional boolean parameter "replace" to NavigationManager.NavigateTo #25752

Closed
Closed
Show file tree
Hide file tree
Changes from 5 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
52 changes: 49 additions & 3 deletions src/Components/Components/src/NavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,47 @@ protected set
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="BaseUri"/>).</param>
/// <param name="forceLoad">If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router.</param>
public void NavigateTo(string uri, bool forceLoad = false)
// 5.0 BACKCOMPAT OVERLOAD -- DO NOT TOUCH
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
public void NavigateTo(string uri, bool forceLoad)
{
NavigateTo(uri, forceLoad, false);
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Navigates to the specified URI.
/// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="BaseUri"/>).</param>
/// <param name="forceLoad">If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router.</param>
/// <param name="replace">If true, will replace the uri in the current browser history state, instead of pushing the new uri onto the browser history stack.</param>
public void NavigateTo(string uri, bool forceLoad = false, bool replace = false)
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
AssertInitialized();
NavigateToCore(uri, new NavigationOptions { ForceLoad = forceLoad, Replace = replace });
}

/// <summary>
/// Navigates to the specified URI.
/// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="BaseUri"/>).</param>
/// <param name="options">Add additional navigation options <see cref="NavigationOptions"/>.</param>
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
public void NavigateTo(string uri, NavigationOptions options)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
AssertInitialized();
NavigateToCore(uri, forceLoad);
NavigateToCore(uri, options);
}

/// <summary>
Expand All @@ -102,7 +139,16 @@ public void NavigateTo(string uri, bool forceLoad = false)
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="BaseUri"/>).</param>
/// <param name="forceLoad">If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router.</param>
protected abstract void NavigateToCore(string uri, bool forceLoad);
[Obsolete("This method is obsolete and will be removed in a future version. Override NavigateToCore(string uri, NavigationOptions options) instead)")]
protected virtual void NavigateToCore(string uri, bool forceLoad) => throw new System.NotImplementedException();
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Navigates to the specified URI.
/// </summary>
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
/// (as returned by <see cref="BaseUri"/>).</param>
/// <param name="options">Add additional navigation options <see cref="NavigationOptions"/>.</param>
protected abstract void NavigateToCore(string uri, NavigationOptions options);
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Called to initialize BaseURI and current URI before these values are used for the first time.
Expand Down
22 changes: 22 additions & 0 deletions src/Components/Components/src/NavigationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.AspNetCore.Components
{
/// <summary>
/// Additional options for navigating to another URI
/// </summary>
public class NavigationOptions
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
///If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router.
/// </summary>
public bool ForceLoad { get; set; }
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
/// If true, will replace the uri in the current browser history state, instead of pushing the new uri onto the browser history stack.
/// </summary>
public bool Replace { get; set; }
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
}
}
15 changes: 12 additions & 3 deletions src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable enable
#nullable enable
Microsoft.AspNetCore.Components.BindConverter
Microsoft.AspNetCore.Components.BindElementAttribute
Microsoft.AspNetCore.Components.BindElementAttribute.BindElementAttribute(string! element, string? suffix, string! valueAttribute, string! changeAttribute) -> void
Expand Down Expand Up @@ -119,13 +119,21 @@ Microsoft.AspNetCore.Components.NavigationManager.BaseUri.get -> string!
Microsoft.AspNetCore.Components.NavigationManager.BaseUri.set -> void
Microsoft.AspNetCore.Components.NavigationManager.Initialize(string! baseUri, string! uri) -> void
Microsoft.AspNetCore.Components.NavigationManager.LocationChanged -> System.EventHandler<Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs!>!
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, bool forceLoad = false) -> void
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, Microsoft.AspNetCore.Components.NavigationOptions! options) -> void
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, bool forceLoad = false, bool replace = false) -> void
Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(string! uri, bool forceLoad) -> void
Microsoft.AspNetCore.Components.NavigationManager.NavigationManager() -> void
Microsoft.AspNetCore.Components.NavigationManager.NotifyLocationChanged(bool isInterceptedLink) -> void
Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string! relativeUri) -> System.Uri!
Microsoft.AspNetCore.Components.NavigationManager.ToBaseRelativePath(string! uri) -> string!
Microsoft.AspNetCore.Components.NavigationManager.Uri.get -> string!
Microsoft.AspNetCore.Components.NavigationManager.Uri.set -> void
Microsoft.AspNetCore.Components.NavigationOptions
Microsoft.AspNetCore.Components.NavigationOptions.ForceLoad.get -> bool
Microsoft.AspNetCore.Components.NavigationOptions.ForceLoad.set -> void
Microsoft.AspNetCore.Components.NavigationOptions.NavigationOptions() -> void
Microsoft.AspNetCore.Components.NavigationOptions.Replace.get -> bool
Microsoft.AspNetCore.Components.NavigationOptions.Replace.set -> void
Microsoft.AspNetCore.Components.OwningComponentBase
Microsoft.AspNetCore.Components.OwningComponentBase.IsDisposed.get -> bool
Microsoft.AspNetCore.Components.OwningComponentBase.OwningComponentBase() -> void
Expand Down Expand Up @@ -299,7 +307,7 @@ abstract Microsoft.AspNetCore.Components.Dispatcher.InvokeAsync(System.Action! w
abstract Microsoft.AspNetCore.Components.Dispatcher.InvokeAsync(System.Func<System.Threading.Tasks.Task!>! workItem) -> System.Threading.Tasks.Task!
abstract Microsoft.AspNetCore.Components.Dispatcher.InvokeAsync<TResult>(System.Func<System.Threading.Tasks.Task<TResult>!>! workItem) -> System.Threading.Tasks.Task<TResult>!
abstract Microsoft.AspNetCore.Components.Dispatcher.InvokeAsync<TResult>(System.Func<TResult>! workItem) -> System.Threading.Tasks.Task<TResult>!
abstract Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(string! uri, bool forceLoad) -> void
abstract Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(string! uri, Microsoft.AspNetCore.Components.NavigationOptions! options) -> void
abstract Microsoft.AspNetCore.Components.RenderTree.Renderer.Dispatcher.get -> Microsoft.AspNetCore.Components.Dispatcher!
abstract Microsoft.AspNetCore.Components.RenderTree.Renderer.HandleException(System.Exception! exception) -> void
abstract Microsoft.AspNetCore.Components.RenderTree.Renderer.UpdateDisplayAsync(in Microsoft.AspNetCore.Components.RenderTree.RenderBatch renderBatch) -> System.Threading.Tasks.Task!
Expand Down Expand Up @@ -410,6 +418,7 @@ virtual Microsoft.AspNetCore.Components.ComponentBase.OnParametersSetAsync() ->
virtual Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters) -> System.Threading.Tasks.Task!
virtual Microsoft.AspNetCore.Components.ComponentBase.ShouldRender() -> bool
virtual Microsoft.AspNetCore.Components.NavigationManager.EnsureInitialized() -> void
virtual Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(string! uri, bool forceLoad) -> void
virtual Microsoft.AspNetCore.Components.OwningComponentBase.Dispose(bool disposing) -> void
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(ulong eventHandlerId, Microsoft.AspNetCore.Components.RenderTree.EventFieldInfo! fieldInfo, System.EventArgs! eventArgs) -> System.Threading.Tasks.Task!
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.Dispose(bool disposing) -> void
Expand Down
5 changes: 1 addition & 4 deletions src/Components/Components/test/NavigationManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ public TestNavigationManager(string baseUri = null, string uri = null)
base.Initialize(baseUri, uri);
}

protected override void NavigateToCore(string uri, bool forceLoad)
{
throw new System.NotImplementedException();
}
protected override void NavigateToCore(string uri, NavigationOptions options) => throw new NotImplementedException();
}
}
}
3 changes: 1 addition & 2 deletions src/Components/Components/test/Routing/RouterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ internal class TestNavigationManager : NavigationManager
{
public TestNavigationManager() =>
Initialize("https://www.example.com/subdir/", "https://www.example.com/subdir/jan");

protected override void NavigateToCore(string uri, bool forceLoad) => throw new NotImplementedException();
protected override void NavigateToCore(string uri, NavigationOptions options) => throw new NotImplementedException();
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
}

internal sealed class TestNavigationInterception : INavigationInterception
Expand Down
15 changes: 7 additions & 8 deletions src/Components/Server/src/Circuits/RemoteNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,29 @@ public void NotifyLocationChanged(string uri, bool intercepted)
}

/// <inheritdoc />
protected override void NavigateToCore(string uri, bool forceLoad)
protected override void NavigateToCore(string uri, NavigationOptions options)
{
Log.RequestingNavigation(_logger, uri, forceLoad);
Log.RequestingNavigation(_logger, uri, options.ForceLoad, options.Replace);

if (_jsRuntime == null)
{
var absoluteUriString = ToAbsoluteUri(uri).ToString();
throw new NavigationException(absoluteUriString);
}

_jsRuntime.InvokeAsync<object>(Interop.NavigateTo, uri, forceLoad);
_jsRuntime.InvokeAsync<object>(Interop.NavigateTo, uri, options.ForceLoad, options.Replace);
}

private static class Log
{
private static readonly Action<ILogger, string, bool, Exception> _requestingNavigation =
LoggerMessage.Define<string, bool>(LogLevel.Debug, new EventId(1, "RequestingNavigation"), "Requesting navigation to URI {Uri} with forceLoad={ForceLoad}");
private static readonly Action<ILogger, string, bool, bool, Exception> _requestingNavigation =
LoggerMessage.Define<string, bool, bool>(LogLevel.Debug, new EventId(1, "RequestingNavigation"), "Requesting navigation to URI {Uri} with forceLoad={ForceLoad} and replace={Replace}");

private static readonly Action<ILogger, string, bool, Exception> _receivedLocationChangedNotification =
LoggerMessage.Define<string, bool>(LogLevel.Debug, new EventId(2, "ReceivedLocationChangedNotification"), "Received notification that the URI has changed to {Uri} with isIntercepted={IsIntercepted}");

public static void RequestingNavigation(ILogger logger, string uri, bool forceLoad)
public static void RequestingNavigation(ILogger logger, string uri, bool forceLoad, bool replace)
{
_requestingNavigation(logger, uri, forceLoad, null);
_requestingNavigation(logger, uri, forceLoad, replace, null);
}

public static void ReceivedLocationChangedNotification(ILogger logger, string uri, bool isIntercepted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,6 @@ internal class TestNavigationManager : NavigationManager
public TestNavigationManager() =>
Initialize("https://www.example.com/base/", "https://www.example.com/base/add-product");

protected override void NavigateToCore(string uri, bool forceLoad) => throw new NotImplementedException();
protected override void NavigateToCore(string uri, NavigationOptions options) => throw new NotImplementedException();
MariovanZeist marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ private class TestNavigationManager : NavigationManager
{
public TestNavigationManager(string baseUrl, string currentUrl) => Initialize(baseUrl, currentUrl);

protected override void NavigateToCore(string uri, bool forceLoad)
protected override void NavigateToCore(string uri, NavigationOptions options)
=> Uri = System.Uri.IsWellFormedUriString(uri, UriKind.Absolute) ? uri : new Uri(new Uri(BaseUri), uri).ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public TestNavigationManager()
Initialize("https://www.example.com/base/", "https://www.example.com/base/counter");
}

protected override void NavigateToCore(string uri, bool forceLoad) => throw new System.NotImplementedException();
protected override void NavigateToCore(string uri, NavigationOptions options) => throw new NotImplementedException();
}

private class TestAuthenticationState : RemoteAuthenticationState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ public void SetLocation(string uri, bool isInterceptedLink)
Uri = uri;
NotifyLocationChanged(isInterceptedLink);
}

/// <inheritdoc />
protected override void NavigateToCore(string uri, bool forceLoad)
protected override void NavigateToCore(string uri, NavigationOptions options)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}

DefaultWebAssemblyJSRuntime.Instance.Invoke<object>(Interop.NavigateTo, uri, forceLoad);
DefaultWebAssemblyJSRuntime.Instance.Invoke<object>(Interop.NavigateTo, uri, options.ForceLoad, options.Replace);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Components;
Expand All @@ -10,9 +10,6 @@ internal class HttpNavigationManager : NavigationManager, IHostEnvironmentNaviga
{
void IHostEnvironmentNavigationManager.Initialize(string baseUri, string uri) => Initialize(baseUri, uri);

protected override void NavigateToCore(string uri, bool forceLoad)
{
throw new NavigationException(uri);
}
protected override void NavigateToCore(string uri, NavigationOptions options) => throw new System.NotImplementedException();
}
}