Redirect to a different page

October 23, 2022

Prerequisite

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-6.0

Story

A page is created with @page /hello/world.

A request is made it to move the page to @page /misc/hello/world. Then the follow up is to redirect the old path to the new path.

Option 1: add another @page

@page /hello/world
@page /misc/hello/world

Oh, the simple redirect is solved. However, it doesn’t scale. It fixes the page but might be difficult to maintain if there are a few places to look for. Quick fix like this can be useful to make it work.

Option 2: redirect at the Router component.

The <Router> provide <NotFound> templated view that displays content. However, we can intercept at the rendering level and use the NavigationManager to navigate to a mapped page.

Client re-direction can be at a single place (a single component). We can express more freely, not bound to the @page directive.

Option 3: replace Router

For a complex routing, we can write a component to interact with NavigationManager and take over the App.razor. This may be the only way when routes are dynamically presented. Such implementation will likely be unique and likely increase maintenance cost a little.

We can still use the Router component as a secondary way of handling if the main lookup fails. However, this will only likely be used in a scenario with 3rd party library integration.

gl hf

Best option is to find the right option for the right use case. As requirements change, keep your options handy. Also, don’t forget to explore other options.


© 2023 Kee Nam