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

Conversation

TheFanatr
Copy link

I've talked about this here.

Essentially, this pull request contains a very simple error 404 handling implementation that allows users to set a fallback route for the blazor routing system to use if the no components are found for the requested route. This is a essentially a catch-all-nonexistent routing handler, where the rendering of the page body is "soft-redirected" to the component represented by the fallback route. The value is set in the App.cshtml file or wherever the router is defined.

<Router AppAssembly=typeof(Program).Assembly FallbackRoute="/404"/>

If FallbackRoute is null (unset), or also points to a nonexistent component, the regular behaviour will be observed, with the only difference being that the exception thrown will say that the router cannot find any component with the fallback route '/<fallback>' if the fallback route is also nonexistent.

The URI displayed by the browser does not change to represent the fallback route in order to allow the fallback component to implement JavaScript interop for handling specific URIs differently, such as the URI for a page that was moved.

@vertonghenb
Copy link
Contributor

I'm wondering if after this implementation it's still possible to navigate to a MVC route not specified in the Blazor Routing system itself.

See: #1154

@TheFanatr
Copy link
Author

@vertonghenb This should still be possible as long as those routes are processed before the Blazor router tries to have them rendered as if they point to components, and/or if an MVC route will override a Blazor route. Will have to test this.

@vertonghenb
Copy link
Contributor

@TheFanatr Alright, love the idea though!

@SteveSandersonMS
Copy link
Member

This looks like a good design to me. Thanks @TheFanatr!

We'll need to add one or more E2E tests like the other router E2E tests. If you're interested in adding that please go ahead and we might be able to merge this sooner. No worries if not, as I'll add them in a week or two when I get to merging this.

@TheFanatr
Copy link
Author

Thanks. I'll look into the E2E tests, but may not end up actually making them before you do.

@TheFanatr
Copy link
Author

@SteveSandersonMS I added an E2E test called CanArriveAtFallbackPageFromBadURI which tries to navigate to the URI for a route that has no components and tests whether or not the fallback route's component was then rendered. I've set the fallback route to point to the default page, as in the / route in TestRouter. It apparently succeeds according to the continuous integration systems, so please take a look and see if there's anything else I need to add and/or change.

@TheFanatr
Copy link
Author

@danroth27 I added a component called Error404 in BasicTestApp and it gets routed to as the fallback on the TestRouter. I also merged master, so everything else is up to date. Please let me know if there is anything else I need to do.

@TheFanatr
Copy link
Author

@SteveSandersonMS should I migrate this to the Razor Components section of the ASP.NET Core repo?

@TheFanatr
Copy link
Author

I've created a pull request on the main repo now.

@SteveSandersonMS
Copy link
Member

Closing in favour of dotnet/aspnetcore#4376

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants