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 2 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
27 changes: 26 additions & 1 deletion src/Components/Components/src/NavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,19 @@ protected set
public void NavigateTo(string uri, bool forceLoad = false)
{
AssertInitialized();
NavigateToCore(uri, forceLoad);
NavigateToCore(uri, new NavigationOptions { ForceLoad = forceLoad });
}

/// <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)
{
AssertInitialized();
NavigateToCore(uri, options);
}

/// <summary>
Expand All @@ -104,6 +116,19 @@ public void NavigateTo(string uri, bool forceLoad = false)
/// <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);
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 virtual void NavigateToCore(string uri, NavigationOptions options)
{
//We call NavigateToCore(uri, options.ForceLoad) so to not introduce a breaking change.
//derived classes should override this function to implement routing
NavigateToCore(uri, options.ForceLoad);
}

/// <summary>
/// Called to initialize BaseURI and current URI before these values are used for the first time.
/// Override <see cref="EnsureInitialized" /> and call this method to dynamically calculate these values.
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 sealed class NavigationOptions
{
/// <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
}
}
18 changes: 11 additions & 7 deletions src/Components/Server/src/Circuits/RemoteNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,32 @@ public void NotifyLocationChanged(string uri, bool intercepted)
/// <inheritdoc />
protected override void NavigateToCore(string uri, bool forceLoad)
{
Log.RequestingNavigation(_logger, uri, forceLoad);
NavigateToCore(uri, new NavigationOptions { ForceLoad = forceLoad });
}

protected override void NavigateToCore(string uri, NavigationOptions options)
{
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 @@ -30,13 +30,23 @@ public void SetLocation(string uri, bool isInterceptedLink)

/// <inheritdoc />
protected override void NavigateToCore(string uri, bool forceLoad)
{
NavigateToCore(uri, new NavigationOptions { ForceLoad = 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);
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

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 Down