Skip to content

Commit f610f23

Browse files
Change fallback param to be a Type
1 parent 96d502e commit f610f23

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

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

Lines changed: 11 additions & 10 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;
@@ -32,9 +31,9 @@ public class Router : IComponent, IDisposable
3231
[Parameter] private Assembly AppAssembly { get; set; }
3332

3433
/// <summary>
35-
/// Gets or sets the route that should be used when no components are found with the requested route.
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.
3635
/// </summary>
37-
[Parameter] private string FallbackRoute { get; set; }
36+
[Parameter] private Type FallbackComponent { get; set; }
3837

3938
private RouteTable Routes { get; set; }
4039

@@ -79,21 +78,23 @@ protected virtual void Render(RenderTreeBuilder builder, Type handler, IDictiona
7978
builder.CloseComponent();
8079
}
8180

82-
private void Refresh(bool useFallback = false)
81+
private void Refresh()
8382
{
84-
var locationPath = useFallback ? FallbackRoute : UriHelper.ToBaseRelativePath(_baseUri, _locationAbsolute);
83+
var locationPath = UriHelper.ToBaseRelativePath(_baseUri, _locationAbsolute);
8584
locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
8685
var context = new RouteContext(locationPath);
8786
Routes.Route(context);
87+
8888
if (context.Handler == null)
8989
{
90-
if (useFallback || FallbackRoute == null)
90+
if (FallbackComponent != null)
91+
{
92+
context.Handler = FallbackComponent;
93+
}
94+
else
9195
{
92-
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with {(useFallback ? "the fallback route" : "a route for")} '/{locationPath}'.");
96+
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}', and no fallback is defined.");
9397
}
94-
95-
Refresh(useFallback: true);
96-
return;
9798
}
9899

99100
if (!typeof(IComponent).IsAssignableFrom(context.Handler))
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
@page "/Error404"
21
<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 FallbackRoute="/Error404" />
1+
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly FallbackComponent="typeof(Error404)" />

0 commit comments

Comments
 (0)