Skip to content

Fallback for Components router (imported from Blazor PR 1534) #4794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Layouts;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Services;
Expand All @@ -30,6 +29,11 @@ public class Router : IComponent, IDisposable
/// assemblies, for components matching the URI.
/// </summary>
[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; }

Expand Down Expand Up @@ -80,9 +84,17 @@ private void Refresh()
locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
var context = new RouteContext(locationPath);
Routes.Route(context);

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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public void CanArriveAtNonDefaultPage()
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]
public void CanFollowLinkToOtherPage()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="test-info">Oops, that component wasn't found!</div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly />
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly FallbackComponent="typeof(Error404)" />