dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.47k stars 10.03k forks source link

Blazor NotFound route causes reload #58886

Closed adelmurad24 closed 5 hours ago

adelmurad24 commented 1 day ago

Describe the bug

Hi!

When using Blazor wasm, the not found route causes browser page reload, happens even in the clear visual studio template. To Reproduce

Create a new blazor wasm(clientside) app with the create a navlink which has a href attribute value a non existing page path. When you click on that navlink it navigates to the right url, but the whole app reloads, like you would open the app on a new page or tab e.g. the loading text shows up then it shows the notfound component.

The router config:

Sorry, there's nothing at this address.

Short time solution

I created a page which catches any page which is not defined but it only works for one path segment

@page "/{Path}"

@Path not found

@code{ [Parameter] public string Path { get; set; } }

for multiple segment paths I would need to define every segment manually because wildcards are not allowed

From the docs: Catch-all parameter syntax (*/**) is not supported in Razor components (.razor). https://docs.microsoft.com/en-us/aspnet/core/blazor/routing?view=aspnetcore-3.1#routing-with-urls-that-contain-dots Further technical details

Visual Studio 2019 dotnet core version 3.1.300

info from the csproj:

netstandard2.1 3.0 service-worker-assets.js
adelmurad24 commented 1 day ago

Please reopen the issue : https://github.com/dotnet/aspnetcore/issues/23100

"If this is a big problem for anyone then in the future we could consider adding a mechanism for saying not to do it, but it's not a priority in the short term."

Yes, it is so important for so many people who only wants to use wasm. So many people are asking about it...

Or please provide a way to override that, or give us a link of the source code of Router / NotFound components

Thanks

javiercn commented 1 day ago

@adelmurad24 thanks for contacting us.

If you want to handle 404's during navigation you can setup a @page /{**slug} to handle any unknown route, and that will prevent the reload.

Within that page you can implement whatever behavior you choose to inside the page, and the app won't reload when hitting an unknown route.

dotnet-policy-service[bot] commented 5 hours ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.