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

Trouble with NavigateTo within OnInitialized #13582

Closed
katiep23 opened this issue Aug 30, 2019 · 10 comments
Closed

Trouble with NavigateTo within OnInitialized #13582

katiep23 opened this issue Aug 30, 2019 · 10 comments
Labels
area-blazor Includes: Blazor, Razor Components question
Milestone

Comments

@katiep23
Copy link

Have following code in my MainLayout.Razor page:

@inject UserState User
@inject IUriHelper uriHelper

@code {
 protected override void OnInitialized()
    {
        base.OnInitialized();
        State.PropertyChanged += OnPropertyChanged;

        if (!User.isLoggedIn)
        {
            User.redirectUrl = uriHelper.GetAbsoluteUri();
            uriHelper.NavigateTo("/Account/Login");
        }
}
}

It worked fine for me, but for others on my team, it gave them "Microsoft.AspNetCore.Components.NavigationException: 'Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.'" on the uriHelper.NavigateTo line when building.

I think this is related to #11591. It shows it resolved. Is there possibly an update the rest of my team does not have? Everybody is using latest version of Visual Studio 2019 Preview & .Net Core 3.0 Preview 8

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Aug 30, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @katiep23.
Is the first-chance exception configuration the same? That is probably the cause here.

@mkArtakMSFT
Copy link
Member

You can try to turn the NavigationException off in the Exceptions dialog (Debug => Windows => Exception Settings) by adding that exception type manually.

@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Aug 30, 2019
@katiep23
Copy link
Author

Nobody has "NavigationException" in their exception settings.

@mkArtakMSFT
Copy link
Member

Go ahead and add the above exception type in the Exception Settings as follows:
image

That will make sure they don't see the exception.

@mkArtakMSFT
Copy link
Member

If you don't add it, the default behavior is to break on the first chance exception and that's what your colleagues are experiencing.

@katiep23
Copy link
Author

So I added it to mine to "break when thrown" & it's still not catching that exception. Is there any reason the exception is being thrown in the first place? If we move "uriHelper.NavigateTo("/Account/Login");" to "OnAfterRender' instead, nobody has the issue.

@mkArtakMSFT
Copy link
Member

@SteveSandersonMS thoughts?

@SteveSandersonMS
Copy link
Member

I was able to use the exception config to control whether or not this causes the debugger to break. Here's my config. Notice that the checkbox is checked, unlike in the screenshot in an earlier comment:

image

With the checkbox checked, the debugger does stop when the redirection occurs. This is probably not the config you want. Everyone on the team should ensure this checkbox is unchecked (and that VS is not configured to break for all Common Language Runtime exceptions), so that it doesn't interfere with page loading all the time.

@SteveSandersonMS
Copy link
Member

Is there any reason the exception is being thrown in the first place?

Yes. When you tell the navigation manager to navigate, the system needs to know to stop the whole prerendering process that is taking place. This is a normal part of the redirect-during-prerendering behavior.

@ghost
Copy link

ghost commented Dec 6, 2019

Thank you for contacting us. Due to no activity on this issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

@ghost ghost closed this as completed Dec 6, 2019
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 6, 2019
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

3 participants