Skip to content

Commit 0be84e4

Browse files
committed
Rise even instead of throwing.
1 parent d6cb325 commit 0be84e4

File tree

6 files changed

+70
-9
lines changed

6 files changed

+70
-9
lines changed

src/Components/Endpoints/src/DependencyInjection/HttpNavigationManager.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ namespace Microsoft.AspNetCore.Components.Endpoints;
77

88
internal sealed class HttpNavigationManager : NavigationManager, IHostEnvironmentNavigationManager
99
{
10+
private const string EnableThrowNavigatioExceptionSwitchKey = "Microsoft.AspNetCore.Components.Endpoints.EnableThrowNavigatioException";
11+
private static readonly bool _throwNavigationException = AppContext.TryGetSwitch(EnableThrowNavigatioExceptionSwitchKey, out var switchValue) && switchValue;
12+
13+
private EventHandler<NavigationEventArgs>? _onNavigateTo;
14+
public event EventHandler<NavigationEventArgs> OnNavigateTo
15+
{
16+
add => _onNavigateTo += value;
17+
remove => _onNavigateTo -= value;
18+
}
19+
1020
void IHostEnvironmentNavigationManager.Initialize(string baseUri, string uri) => Initialize(baseUri, uri);
1121

1222
protected override void NavigateToCore(string uri, NavigationOptions options)
1323
{
1424
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
15-
throw new NavigationException(absoluteUriString);
25+
if (_throwNavigationException)
26+
{
27+
throw new NavigationException(absoluteUriString);
28+
}
29+
else
30+
{
31+
_onNavigateTo?.Invoke(this, new NavigationEventArgs(absoluteUriString));
32+
}
1633
}
1734
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components.Endpoints;
5+
6+
internal class NavigationEventArgs : EventArgs
7+
{
8+
public string Uri { get; }
9+
10+
public NavigationEventArgs(string uri)
11+
{
12+
Uri = uri;
13+
}
14+
}

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ internal async Task InitializeStandardComponentServicesAsync(
8484
if (navigationManager != null)
8585
{
8686
navigationManager.OnNotFound += SetNotFoundResponse;
87+
if (navigationManager is HttpNavigationManager httpNavigationManager)
88+
{
89+
httpNavigationManager.OnNavigateTo += OnNavigateTo;
90+
}
8791
}
8892

8993
var authenticationStateProvider = httpContext.RequestServices.GetService<AuthenticationStateProvider>();
@@ -135,6 +139,11 @@ internal async Task InitializeStandardComponentServicesAsync(
135139
}
136140
}
137141

142+
private void OnNavigateTo(object? sender, NavigationEventArgs args)
143+
{
144+
_httpContext.Response.Redirect(args.Uri);
145+
}
146+
138147
private static void InitializeResourceCollection(HttpContext httpContext)
139148
{
140149

src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@ public void CanUseServerAuthenticationStateByDefault()
6464
Browser.Equal("True", () => Browser.FindElement(By.Id("is-in-test-role-1")).Text);
6565
Browser.Equal("True", () => Browser.FindElement(By.Id("is-in-test-role-2")).Text);
6666
}
67+
68+
[Fact]
69+
public async Task NavigatesWithoutInteractivityByRequestRedirection()
70+
{
71+
Navigate($"{ServerPathBase}/routing/ssr-navigate-to");
72+
await Task.Delay(TimeSpan.FromSeconds(30));
73+
Browser.Equal("Click submit to navigate to home", () => Browser.Exists(By.Id("test-info")).Text);
74+
Browser.Click(By.Id("redirectButton"));
75+
Browser.Equal("Routing test cases", () => Browser.Exists(By.Id("test-info")).Text);
76+
}
6777
}

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Routing/RoutingTestCases.razor

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/routing"
2-
<h3>Routing test cases</h3>
2+
<h3 id="test-info">Routing test cases</h3>
33

44
<ul>
55
<li>
@@ -24,12 +24,6 @@
2424
<a href="routing/complex-segment(value)">Complex segments</a>
2525
</li>
2626
<li>
27-
<a href="routing/not-found-ssr">Not found page for Static Server Rendering</a>
28-
</li>
29-
<li>
30-
<a href="routing/not-found-webassembly">Not found page for Interactive WebAssembly rendering</a>
31-
</li>
32-
<li>
33-
<a href="routing/not-found-server">Not found page for Interactive Server rendering</a>
27+
<a href="routing/ssr-navigate-to">Redirect on SSR page</a>
3428
</li>
3529
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@page "/routing/ssr-navigate-to"
2+
@using Microsoft.AspNetCore.Components.Forms
3+
@inject NavigationManager NavigationManager
4+
5+
<p id="test-info">Click submit to navigate to home</p>
6+
<form method="post" @onsubmit="Submit" @formname="MyUniqueFormName">
7+
<AntiforgeryToken />
8+
<button type="submit" id="redirectButton" class="btn btn-primary">Redirect</button>
9+
</form>
10+
11+
@code {
12+
private void Submit()
13+
{
14+
NavigationManager.NavigateTo("/subdir/routing");
15+
}
16+
}
17+

0 commit comments

Comments
 (0)