Skip to content

Commit 3757908

Browse files
Fallback for Components router (imported from Blazor PR 1534) (#4794)
1 parent e768a78 commit 3757908

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/Components/src/Microsoft.AspNetCore.Components/Routing/Router.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Reflection;
7-
using Microsoft.AspNetCore.Components;
87
using Microsoft.AspNetCore.Components.Layouts;
98
using Microsoft.AspNetCore.Components.RenderTree;
109
using Microsoft.AspNetCore.Components.Services;
@@ -30,6 +29,11 @@ public class Router : IComponent, IDisposable
3029
/// assemblies, for components matching the URI.
3130
/// </summary>
3231
[Parameter] private Assembly AppAssembly { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets the type of the component that should be used as a fallback when no match is found for the requested route.
35+
/// </summary>
36+
[Parameter] private Type FallbackComponent { get; set; }
3337

3438
private RouteTable Routes { get; set; }
3539

@@ -80,9 +84,17 @@ private void Refresh()
8084
locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
8185
var context = new RouteContext(locationPath);
8286
Routes.Route(context);
87+
8388
if (context.Handler == null)
8489
{
85-
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}'.");
90+
if (FallbackComponent != null)
91+
{
92+
context.Handler = FallbackComponent;
93+
}
94+
else
95+
{
96+
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}', and no fallback is defined.");
97+
}
8698
}
8799

88100
if (!typeof(IComponent).IsAssignableFrom(context.Handler))

src/Components/test/Microsoft.AspNetCore.Components.E2ETest/Tests/RoutingTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public void CanArriveAtNonDefaultPage()
8888
AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)");
8989
}
9090

91+
[Fact]
92+
public void CanArriveAtFallbackPageFromBadURI()
93+
{
94+
SetUrlViaPushState("/Oopsie_Daisies%20%This_Aint_A_Real_Page");
95+
96+
var app = MountTestComponent<TestRouter>();
97+
Assert.Equal("Oops, that component wasn't found!", app.FindElement(By.Id("test-info")).Text);
98+
}
99+
91100
[Fact]
92101
public void CanFollowLinkToOtherPage()
93102
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="test-info">Oops, that component wasn't found!</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly />
1+
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly FallbackComponent="typeof(Error404)" />

0 commit comments

Comments
 (0)