dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.

Home Page:https://asp.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NavigationException thrown in Static Server Render

mohiyo opened this issue · comments

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)

@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.

@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.