Skip to content

Blazor SSR Navigate does not work inside try/catch block #57311

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
1 task done
mrpmorris opened this issue Aug 13, 2024 · 1 comment
Closed
1 task done

Blazor SSR Navigate does not work inside try/catch block #57311

mrpmorris opened this issue Aug 13, 2024 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@mrpmorris
Copy link

mrpmorris commented Aug 13, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In a Blazor SSR app, NavigationManager.NavigateTo always throws an exception.

The problem with this approach is when debugging async await methods in Visual Studio the debugger does not stop to show this exception, so the developer is unaware.

It is therefore likely the developer will include the call to NavigateTo within a try block, and then navigation fails...

    private async Task Save()
    {
        try
        {
            await Task.CompletedTask; // A task that may throw an exception
            NavigationManager.NavigateTo("/success");
        }
        catch 
        {
            // Log the exception and show an error to the user
        }
    }

Expected Behavior

It should not throw an exception.

I should be able to write the same code as I would for Server or WASM Blazor apps.

Steps To Reproduce

// VS will stop and show the exception
private void Example1()
{
  NavigationManager.NavigateTo("/eggs");
}

// But I need some async code in my save handler
// VS will NOT stop and show the exception
private async Task Example2()
{
  await Task.CompletedTask; // My complex task
  NavigationManager.NavigateTo("/eggs");
}

// My own async code might throw an exception, so I
// wrap it in a try/catch to tell the user the operation failed
// This will successfully execute the task, but never navigate
private async Task Example3()
{
  ErrorMessage = null;
  try
  {
      await Task.CompletedTask; // My complex task
      NavigationManager.NavigateTo("/eggs");
   }
  catch
  {
    ErrorMessage = "The operation failed";
  }
}

Exceptions (if any)

Microsoft.AspNetCore.Components.NavigationException

.NET Version

8.0.303

Anything else?

No response

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

Thanks for reaching out. This is by design - NavigationException is intended to be caught by the framework to be handled as a redirect. Our guidance is to not catch that exception. We currently have #55685 tracking an alternative approach to initiating redirects.

@MackinnonBuck MackinnonBuck closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2024
@MackinnonBuck MackinnonBuck added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Aug 13, 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 ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants