Skip to content

Navigation Exception: Introduce Clear Message #56644

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 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions src/Components/Components/src/NavigationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public NavigationException(string uri)
Location = uri;
}

/// <summary>
/// Initializes a new <see cref="NavigationException"/> instance with a custom message.
/// </summary>
public NavigationException(string uri, string message) : base(message)
{
Location = uri;
}

/// <summary>
/// Gets the uri to which navigation was attempted.
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions src/Components/Components/src/NavigationRedirectHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.Components;

/// <summary>
/// Helper class to throw a <see cref="NavigationException"/>.
/// </summary>
public static class NavigationRedirectHelper
{
private const string RedirectExceptionMessage =
"A navigation was initiated during static rendering. " +
"This exception is not an error, but instead signals to the framework that a redirect should occur. " +
"It should not be caught by application code.";

/// <summary>
/// Creates an absolute URI and throws a new <see cref="NavigationException"/>.
/// </summary>

[DoesNotReturn]
public static void ThrowNavigationExceptionForRedirect(NavigationManager navigationManager, string uri)
{
var absoluteUriString = navigationManager.ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString, RedirectExceptionMessage);
}
}
3 changes: 3 additions & 0 deletions src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Microsoft.AspNetCore.Components.ComponentBase.AssignedRenderMode.get -> Microsof
Microsoft.AspNetCore.Components.ComponentBase.RendererInfo.get -> Microsoft.AspNetCore.Components.RendererInfo!
Microsoft.AspNetCore.Components.ExcludeFromInteractiveRoutingAttribute
Microsoft.AspNetCore.Components.ExcludeFromInteractiveRoutingAttribute.ExcludeFromInteractiveRoutingAttribute() -> void
Microsoft.AspNetCore.Components.NavigationException.NavigationException(string! uri, string! message) -> void
Microsoft.AspNetCore.Components.NavigationRedirectHelper
Microsoft.AspNetCore.Components.RendererInfo
Microsoft.AspNetCore.Components.RendererInfo.IsInteractive.get -> bool
Microsoft.AspNetCore.Components.RendererInfo.Name.get -> string!
Expand All @@ -23,6 +25,7 @@ Microsoft.AspNetCore.Components.ResourceAssetProperty
Microsoft.AspNetCore.Components.ResourceAssetProperty.Name.get -> string!
Microsoft.AspNetCore.Components.ResourceAssetProperty.ResourceAssetProperty(string! name, string! value) -> void
Microsoft.AspNetCore.Components.ResourceAssetProperty.Value.get -> string!
static Microsoft.AspNetCore.Components.NavigationRedirectHelper.ThrowNavigationExceptionForRedirect(Microsoft.AspNetCore.Components.NavigationManager! navigationManager, string! uri) -> void
static readonly Microsoft.AspNetCore.Components.ResourceAssetCollection.Empty -> Microsoft.AspNetCore.Components.ResourceAssetCollection!
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.Assets.get -> Microsoft.AspNetCore.Components.ResourceAssetCollection!
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.RendererInfo.get -> Microsoft.AspNetCore.Components.RendererInfo!
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal sealed class HttpNavigationManager : NavigationManager, IHostEnvironmen

protected override void NavigateToCore(string uri, NavigationOptions options)
{
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
NavigationRedirectHelper.ThrowNavigationExceptionForRedirect(this, uri);
}
}
6 changes: 2 additions & 4 deletions src/Components/Server/src/Circuits/RemoteNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ protected override void NavigateToCore(string uri, NavigationOptions options)

if (_jsRuntime == null)
{
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
NavigationRedirectHelper.ThrowNavigationExceptionForRedirect(this, uri);
}

_ = PerformNavigationAsync();
Expand Down Expand Up @@ -128,8 +127,7 @@ public override void Refresh(bool forceReload = false)
{
if (_jsRuntime == null)
{
var absoluteUriString = ToAbsoluteUri(Uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
NavigationRedirectHelper.ThrowNavigationExceptionForRedirect(this, Uri);
}

_ = RefreshAsync();
Expand Down
Loading