|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Reflection;
|
7 |
| -using Microsoft.AspNetCore.Components; |
8 | 7 | using Microsoft.AspNetCore.Components.Layouts;
|
9 | 8 | using Microsoft.AspNetCore.Components.RenderTree;
|
10 | 9 | using Microsoft.AspNetCore.Components.Services;
|
@@ -32,9 +31,9 @@ public class Router : IComponent, IDisposable
|
32 | 31 | [Parameter] private Assembly AppAssembly { get; set; }
|
33 | 32 |
|
34 | 33 | /// <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. |
36 | 35 | /// </summary>
|
37 |
| - [Parameter] private string FallbackRoute { get; set; } |
| 36 | + [Parameter] private Type FallbackComponent { get; set; } |
38 | 37 |
|
39 | 38 | private RouteTable Routes { get; set; }
|
40 | 39 |
|
@@ -79,21 +78,23 @@ protected virtual void Render(RenderTreeBuilder builder, Type handler, IDictiona
|
79 | 78 | builder.CloseComponent();
|
80 | 79 | }
|
81 | 80 |
|
82 |
| - private void Refresh(bool useFallback = false) |
| 81 | + private void Refresh() |
83 | 82 | {
|
84 |
| - var locationPath = useFallback ? FallbackRoute : UriHelper.ToBaseRelativePath(_baseUri, _locationAbsolute); |
| 83 | + var locationPath = UriHelper.ToBaseRelativePath(_baseUri, _locationAbsolute); |
85 | 84 | locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
|
86 | 85 | var context = new RouteContext(locationPath);
|
87 | 86 | Routes.Route(context);
|
| 87 | + |
88 | 88 | if (context.Handler == null)
|
89 | 89 | {
|
90 |
| - if (useFallback || FallbackRoute == null) |
| 90 | + if (FallbackComponent != null) |
| 91 | + { |
| 92 | + context.Handler = FallbackComponent; |
| 93 | + } |
| 94 | + else |
91 | 95 | {
|
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."); |
93 | 97 | }
|
94 |
| - |
95 |
| - Refresh(useFallback: true); |
96 |
| - return; |
97 | 98 | }
|
98 | 99 |
|
99 | 100 | if (!typeof(IComponent).IsAssignableFrom(context.Handler))
|
|
0 commit comments