Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Simple 404 Handling (Fallback Route) #1534

Closed
wants to merge 13 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
19 changes: 15 additions & 4 deletions src/Microsoft.AspNetCore.Components/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class Router : IComponent, IDisposable
/// </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; }

/// <inheritdoc />
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 All @@ -103,4 +114,4 @@ private void OnLocationChanged(object sender, string newAbsoluteUri)
}
}
}
}
}
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
2 changes: 2 additions & 0 deletions test/testapps/BasicTestApp/RouterTest/Error404.cshtml
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>
2 changes: 1 addition & 1 deletion test/testapps/BasicTestApp/RouterTest/TestRouter.cshtml
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" />