Skip to content

[Components] Simple 404 Handling (Fallback Route) #4376

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -30,6 +30,11 @@ public class Router : IComponent, IDisposable
/// assemblies, for components matching the URI.
/// </summary>
[Parameter] private Assembly AppAssembly { get; set; }

/// <summary>
/// Gets or sets the route that should be used when no components are found with the requested route.
/// </summary>
[Parameter] private string FallbackRoute { get; set; }

private RouteTable Routes { get; set; }

Expand Down Expand Up @@ -74,15 +79,21 @@ protected virtual void Render(RenderTreeBuilder builder, Type handler, IDictiona
builder.CloseComponent();
}

private void Refresh()
private void Refresh(bool useFallback = false)
{
var locationPath = UriHelper.ToBaseRelativePath(_baseUri, _locationAbsolute);
var locationPath = useFallback ? FallbackRoute : UriHelper.ToBaseRelativePath(_baseUri, _locationAbsolute);
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 (useFallback || FallbackRoute == null)
{
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with {(useFallback ? "the fallback route" : "a route for")} '/{locationPath}'.");
}

Refresh(useFallback: true);
return;
}

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,2 @@
@page "/Error404"
<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 FallbackRoute="/Error404" />