Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavigateTo within OnInitialized not working #28355

Closed
KurtP20 opened this issue Dec 3, 2020 · 3 comments
Closed

NavigateTo within OnInitialized not working #28355

KurtP20 opened this issue Dec 3, 2020 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@KurtP20
Copy link

KurtP20 commented Dec 3, 2020

Describe the bug

Following the advice give here, I added RedirectToLogin.razor in my Blazor Server Side App. Unfortunately, NavigateTo throws an exception when placed inside OnInitialized. It is a known problem and e. g. in this SO question it is recommended to place it in OnAfterRender or disable server prerendering. The SO question also referrs to issues (#11591 and #13582) which are marked as closed, but the problem still exists.

To Reproduce

Use this git repository which is a Blazor Server Side App with Identity and the RedirectToLogin and the access-protected Counter page. Start the app and add '/counter' to the URL (clicking the 'Counter' menu item does not show the same exception) which will call ´RedirectToLogin´ and throw the exception.
Please note, that ´NavigateTo´ works despite throwing the exception.

Exceptions (if any)

With '/counter' in the URL: 'Microsoft.AspNetCore.Components.NavigationException' in Microsoft.AspNetCore.Components.dll

With the 'Counter' button: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll (might not be related to my question, but is still interesting that an exception is thrown)

Further technical details

  • ASP.NET Core version 5.0
  • Include the output of dotnet --info

.NET SDK (reflecting any global.json):
Version: 5.0.100
Commit: 5044b93829

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
Version: 5.0.0
Commit: cf258a14b7

.NET SDKs installed:
2.1.801 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]
3.1.301 [C:\Program Files\dotnet\sdk]
5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    Microsoft Visual Studio Enterprise 2019, 16.8.2
@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Dec 3, 2020
@javiercn
Copy link
Member

javiercn commented Dec 3, 2020

@KurtP20 thanks for contacting us.

This is a well-known exception that the component throws and the componentrenderer catches in order to perform the redirection during prerendering. It should not prevent the app from working at all.

If that's not the case, please let us know.

@javiercn javiercn added ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question labels Dec 3, 2020
@ghost ghost added the Status: Resolved label Dec 3, 2020
@KurtP20
Copy link
Author

KurtP20 commented Dec 3, 2020

Thanks for taking the time to answer. As far as I can tell, the app workes fine. I did not know that it is safe to ignore the exception, and my google search also did not bring up a clear answer. So you say, that it is safe to use ´NavigateTo´ in ´OnInitialized´ and ignore the excetion that might stop the debugger, or would you still recommend to place it in ´OnAfterRender´?

@javiercn
Copy link
Member

javiercn commented Dec 3, 2020

Yes, it is completely safe to ignore it. The debugger stops because it is configured to do so, but in this case the exception is always handled. You can turn-off breaking on this exception if it's caught on the debugger settings.

@KurtP20 KurtP20 closed this as completed Dec 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants