Skip to content

NavigationException thrown in Static Server Render #55685

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

Closed
mohiyo opened this issue May 13, 2024 · 7 comments
Closed

NavigationException thrown in Static Server Render #55685

mohiyo opened this issue May 13, 2024 · 7 comments
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Pillar: Dev Experience Status: Resolved

Comments

@mohiyo
Copy link

mohiyo commented May 13, 2024

I have a Login Form inside a Blazor Server App which is working fine with built-in functionality.
But now I am trying to solve a problem where I will be allowing Users to log in automatically when they reach from certain URL and with certain parameters, Without submitting a form

I tried this in my Login Component

protected override async Task OnInitializedAsync() {
  try {
    if (HttpMethods.IsGet(HttpContext.Request.Method))
      await HttpContext.SignOutAsync();

    if (parameter && condition) {
      await DirectLogin();
    }
  } catch (Exception ex) {
    _logger.LogError(message: ex.Message);
  }
}

async Task DirectLogin() {
  try {
  //Perform some logic here and then SignIn

    await HttpContext.SignInAsync(principal, new AuthenticationProperties {
      ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(5)
    });

    _navigation.NavigateTo($"/direct/page/{parameter}", true);
    //Here I get this exception "Microsoft.AspNetCore.Components.NavigationException"

  } catch (Exception ex) {
    errorMessage = $ "Error: {ex.Message}";
    _logger.LogError(message: ex.Message);
  }
}

This login Component is in a Static Server Render mode whereas after the successful SignIn, the User will be redirected to an Interactive Server Component.

But Navigation is not working and I am getting this exception

Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown

Originally posted by @mohiyo in #49472 (comment)

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label May 13, 2024
@javiercn
Copy link
Member

@mohiyo thanks for contacting us.

What's likely happening is that you are breaking on first chance exceptions. NavigationException is thrown by the framework to trigger an SSR navigation, but it's caught internally.

You should avoid capturing NavigationException.

@mohiyo
Copy link
Author

mohiyo commented May 13, 2024

@javiercn even if I don't capture the exception it still gives this error. Can you point out what's the real reason for this exception?

I never had Try/Catch block until I faced this exception, I want to get some sort of Inner Exception.

@mkArtakMSFT mkArtakMSFT added enhancement This issue represents an ask for new feature or an enhancement to an existing one Pillar: Dev Experience labels May 13, 2024
@mkArtakMSFT mkArtakMSFT added this to the .NET 10 Planning milestone May 13, 2024
@sbwalker
Copy link

@mohiyo I believe this is the same issue as logged here: #53996

@HuaFangYun
Copy link

I would like to ask how to deal with this problem. At present, it is only reported as "Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown." There is no hint of anything else.

@bulsond
Copy link

bulsond commented Jun 1, 2024

image

@mkArtakMSFT
Copy link
Contributor

mkArtakMSFT commented Nov 5, 2024

@halter73 isn't this something that will be addressed as part of your PR ?

@mkArtakMSFT
Copy link
Contributor

Confirmed with @halter73 that the above linked PR is relevant, and it will improve the debugging experience.
Also, #53996 will allow us to not throw the exception in the first place.

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Nov 12, 2024
@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Pillar: Dev Experience Status: Resolved
Projects
None yet
Development

No branches or pull requests

6 participants