Fallback for Components router (imported from Blazor PR 1534) (#4794)
This commit is contained in:
parent
e768a78c2b
commit
3757908b14
|
|
@ -4,7 +4,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Components;
|
|
||||||
using Microsoft.AspNetCore.Components.Layouts;
|
using Microsoft.AspNetCore.Components.Layouts;
|
||||||
using Microsoft.AspNetCore.Components.RenderTree;
|
using Microsoft.AspNetCore.Components.RenderTree;
|
||||||
using Microsoft.AspNetCore.Components.Services;
|
using Microsoft.AspNetCore.Components.Services;
|
||||||
|
|
@ -30,6 +29,11 @@ namespace Microsoft.AspNetCore.Components.Routing
|
||||||
/// assemblies, for components matching the URI.
|
/// assemblies, for components matching the URI.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Parameter] private Assembly AppAssembly { get; set; }
|
[Parameter] private Assembly AppAssembly { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the type of the component that should be used as a fallback when no match is found for the requested route.
|
||||||
|
/// </summary>
|
||||||
|
[Parameter] private Type FallbackComponent { get; set; }
|
||||||
|
|
||||||
private RouteTable Routes { get; set; }
|
private RouteTable Routes { get; set; }
|
||||||
|
|
||||||
|
|
@ -80,9 +84,17 @@ namespace Microsoft.AspNetCore.Components.Routing
|
||||||
locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
|
locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
|
||||||
var context = new RouteContext(locationPath);
|
var context = new RouteContext(locationPath);
|
||||||
Routes.Route(context);
|
Routes.Route(context);
|
||||||
|
|
||||||
if (context.Handler == null)
|
if (context.Handler == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}'.");
|
if (FallbackComponent != null)
|
||||||
|
{
|
||||||
|
context.Handler = FallbackComponent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}', and no fallback is defined.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!typeof(IComponent).IsAssignableFrom(context.Handler))
|
if (!typeof(IComponent).IsAssignableFrom(context.Handler))
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,15 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
|
||||||
AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)");
|
AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanArriveAtFallbackPageFromBadURI()
|
||||||
|
{
|
||||||
|
SetUrlViaPushState("/Oopsie_Daisies%20%This_Aint_A_Real_Page");
|
||||||
|
|
||||||
|
var app = MountTestComponent<TestRouter>();
|
||||||
|
Assert.Equal("Oops, that component wasn't found!", app.FindElement(By.Id("test-info")).Text);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanFollowLinkToOtherPage()
|
public void CanFollowLinkToOtherPage()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
<div id="test-info">Oops, that component wasn't found!</div>
|
||||||
|
|
@ -1 +1 @@
|
||||||
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly />
|
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly FallbackComponent="typeof(Error404)" />
|
||||||
Loading…
Reference in New Issue