In an ASP.NET Blazor App with Identity I have a page with the page routing
@page "/{PageName}"
When I change the page routing of the login page in Areas/Identity/Pages/Login.cshtml to
@page "/login"
and enter the URL ‘localhost:44397/login’, the login page shows up as expected.
However, if I follow the link in header bar that I have adapted in LoginDisplay.razor to <a href="login">Log in</a>, the URL changes to ‘localhost:44397/login’ as expected, but the login page does not appear. Instead, the above page is rendered, where the PageName parameter is set to ‘login’.
Endpoint routing obviously works if the URL is entered manually but fails when clicking a link, as if half of the routing middleware would have been bypassed. I tested this with a fresh Blazor template and have no more ideas where to look for the bug.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Accoring to this article, a click on a hyperlink in a Blazor component is intercepted automatically. While the URL in the browser is updated, the request is not sent to the browser but the page is renderd by Blazor. In my case, Blazor can find a suitable page (the one with the @page "/{PageName}" directive) and renders it.
To force loading of the page, NavigationManager can be used:
<a href="login" @onclick="@(() => navigationManager.NavigateTo("/login", true))">Login</a>
The href is necessary to render the link with the typical layout. It’s probably not the most elegant solution but at least it works.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0