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

HttpNavigationManager no longer uses NavigationException #61306

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Microsoft.AspNetCore.Components.SupplyParameterFromPersistentComponentStateAttri
Microsoft.AspNetCore.Components.SupplyParameterFromPersistentComponentStateAttribute.SupplyParameterFromPersistentComponentStateAttribute() -> void
Microsoft.Extensions.DependencyInjection.SupplyParameterFromPersistentComponentStateProviderServiceCollectionExtensions
static Microsoft.AspNetCore.Components.Infrastructure.RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<TService>(Microsoft.Extensions.DependencyInjection.IServiceCollection! services, Microsoft.AspNetCore.Components.IComponentRenderMode! componentRenderMode) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static Microsoft.Extensions.DependencyInjection.SupplyParameterFromPersistentComponentStateProviderServiceCollectionExtensions.AddSupplyValueFromPersistentComponentStateProvider(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static Microsoft.Extensions.DependencyInjection.SupplyParameterFromPersistentComponentStateProviderServiceCollectionExtensions.AddSupplyValueFromPersistentComponentStateProvider(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@ namespace Microsoft.AspNetCore.Components.Endpoints;

internal sealed class HttpNavigationManager : NavigationManager, IHostEnvironmentNavigationManager
{
private const string _enableThrowNavigationException = "Microsoft.AspNetCore.Components.Endpoints.HttpNavigationManager.EnableThrowNavigationException";

private static bool _throwNavigationException =>
AppContext.TryGetSwitch(_enableThrowNavigationException, out var switchValue) && switchValue;

void IHostEnvironmentNavigationManager.Initialize(string baseUri, string uri) => Initialize(baseUri, uri);

protected override void NavigateToCore(string uri, NavigationOptions options)
{
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
if (_throwNavigationException)
{
throw new NavigationException(absoluteUriString);
}
else
{
Uri = absoluteUriString;
NotifyLocationChanged(isInterceptedLink: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ internal async Task InitializeStandardComponentServicesAsync(
if (navigationManager != null)
{
navigationManager.OnNotFound += SetNotFoundResponse;
navigationManager.LocationChanged += OnNavigateTo;
}

var authenticationStateProvider = httpContext.RequestServices.GetService<AuthenticationStateProvider>();
Expand Down Expand Up @@ -135,6 +136,11 @@ internal async Task InitializeStandardComponentServicesAsync(
}
}

private void OnNavigateTo(object? sender, LocationChangedEventArgs args)
{
_httpContext.Response.Redirect(args.Location);
}

private static void InitializeResourceCollection(HttpContext httpContext)
{

Expand Down
23 changes: 21 additions & 2 deletions src/Components/Server/src/Circuits/RemoteNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal sealed partial class RemoteNavigationManager : NavigationManager, IHost
private readonly ILogger<RemoteNavigationManager> _logger;
private IJSRuntime _jsRuntime;
private bool? _navigationLockStateBeforeJsRuntimeAttached;
private const string _enableThrowNavigationException = "Microsoft.AspNetCore.Components.Endpoints.HttpNavigationManager.EnableThrowNavigationException";
private static bool _throwNavigationException =>
AppContext.TryGetSwitch(_enableThrowNavigationException, out var switchValue) && switchValue;

public event EventHandler<Exception>? UnhandledException;

Expand Down Expand Up @@ -88,7 +91,15 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
if (_jsRuntime == null)
{
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
if (_throwNavigationException)
{
throw new NavigationException(absoluteUriString);
}
else
{
Uri = absoluteUriString;
NotifyLocationChanged(isInterceptedLink: false);
}
}

_ = PerformNavigationAsync();
Expand Down Expand Up @@ -129,7 +140,15 @@ public override void Refresh(bool forceReload = false)
if (_jsRuntime == null)
{
var absoluteUriString = ToAbsoluteUri(Uri).AbsoluteUri;
throw new NavigationException(absoluteUriString);
if (_throwNavigationException)
{
throw new NavigationException(absoluteUriString);
}
else
{
Uri = absoluteUriString;
NotifyLocationChanged(isInterceptedLink: false);
}
}

_ = RefreshAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,4 +1399,16 @@ public void CanPersistMultiplePrerenderedStateDeclaratively_Auto_PersistsOnWebAs
Browser.Equal("restored 2", () => Browser.FindElement(By.Id("auto-2")).Text);
Browser.Equal("WebAssembly", () => Browser.FindElement(By.Id("render-mode-auto-2")).Text);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void NavigatesWithInteractivityByRequestRedirection(bool controlFlowByException)
{
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.EnableThrowNavigationException", isEnabled: controlFlowByException);
Navigate($"{ServerPathBase}/routing/ssr-navigate-to");
Browser.Equal("Click submit to navigate to home", () => Browser.Exists(By.Id("test-info")).Text);
Browser.Click(By.Id("redirectButton"));
Browser.Equal("Routing test cases", () => Browser.Exists(By.Id("test-info")).Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ public void CanUseServerAuthenticationStateByDefault()
Browser.Equal("True", () => Browser.FindElement(By.Id("is-in-test-role-1")).Text);
Browser.Equal("True", () => Browser.FindElement(By.Id("is-in-test-role-2")).Text);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void NavigatesWithoutInteractivityByRequestRedirection(bool controlFlowByException)
{
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.EnableThrowNavigationException", isEnabled: controlFlowByException);
Navigate($"{ServerPathBase}/routing/ssr-navigate-to");
Browser.Equal("Click submit to navigate to home", () => Browser.Exists(By.Id("test-info")).Text);
Browser.Click(By.Id("redirectButton"));
Browser.Equal("Routing test cases", () => Browser.Exists(By.Id("test-info")).Text);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/routing"
<h3>Routing test cases</h3>
<h3 id="test-info">Routing test cases</h3>

<ul>
<li>
Expand All @@ -24,12 +24,6 @@
<a href="routing/complex-segment(value)">Complex segments</a>
</li>
<li>
<a href="routing/not-found-ssr">Not found page for Static Server Rendering</a>
</li>
<li>
<a href="routing/not-found-webassembly">Not found page for Interactive WebAssembly rendering</a>
</li>
<li>
<a href="routing/not-found-server">Not found page for Interactive Server rendering</a>
<a href="routing/ssr-navigate-to">Redirect on SSR page</a>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@page "/routing/ssr-navigate-to"
@using Microsoft.AspNetCore.Components.Forms
@inject NavigationManager NavigationManager

<p id="test-info">Click submit to navigate to home</p>
<form method="post" @onsubmit="Submit" @formname="MyUniqueFormName">
<AntiforgeryToken />
<button type="submit" id="redirectButton" class="btn btn-primary">Redirect</button>
</form>

@code {
private void Submit()
{
NavigationManager.NavigateTo("/subdir/routing");
}
}

Loading